androidandroid-notificationswear-osandroid-wear-notification

Android wearable stacked notifications


I was playing with stacked notifications but I can't make it work, the notifications don't fire at all. Here's the code:

    private void sendSimpleStackedNotifications() {
        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
                .setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.notif_background));

        for (int i = 0; i < 5; i++) {
           ...              
        }
    }

Inside the for loop I have:
Version 1:

Notification n = new NotificationCompat.Builder(this)
                        .setContentTitle("New notification!")
                        .setContentText("Notification nº" + (i + 1))
                        .extend(wearableExtender)
                        .setGroup(GROUP)
                        .build();
mNotificationManager.notify(i, n);

Version 2:

NotificationCompat.Builder nb = new NotificationCompat.Builder(this)
                        .setContentTitle("New notification!")
                        .setContentText("Notification nº" + (i + 1))
                        .extend(wearableExtender)
                        .setGroup(GROUP);        
mNotificationManager.notify(i, nb.build());

But none of the approaches work. What am I missing?

EDIT: Thanks to user aiur I've found what I was missing:

.setSmallIcon()

Now the notifications are correctly shown but I have a problem, they are not grouped in the hand-held device even if I add (in both Version 1 and Version 2):

.setGroup(GROUP)
.setGroupSummary(true)

In the wearable they are correctly stacked.

Any idea why?
Thanks.


Solution

  • You need to set SmallIcon for Notification

            for(int i = 0 ; i < 5 ; i++){
            Notification n = new NotificationCompat.Builder(this)
                    .setContentTitle("New notification!")
                    .setContentText("Notification nº" + i + 1)
                    .extend(wearableExtender)
                    .setGroup(GROUP)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
            mNotificationManager.notify(i, n);
        }
    

    Maybe you'll need a summaryNotification

    private void sendNotification(){
        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
                .setBackground(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    
        NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(this);
    
        String GROUP = "group";
    
        //send stack Notification (wearable)
        for(int i = 0 ; i < 5 ; i++){
            Notification n = new NotificationCompat.Builder(this)
                    .setContentTitle("New notification!")
                    .setContentText("Notification nº" + i + 1)
                    .extend(wearableExtender)
                    .setGroup(GROUP)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
            mNotificationManager.notify(i, n);
        }
    
        //send summary notification (only handheld)
        NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(this)
                .setGroupSummary(true)
                .setGroup(GROUP)
                .setContentText("New notification!")
                .setContentTitle("5 New Notification!")
                .setSmallIcon(R.mipmap.ic_launcher);
    
        mNotificationManager.notify(-1 , summaryNotification.build());
    }
    

    "It's important that you still provide a summary notification that appears on handheld devices. So in addition to adding each unique notification to the same stack group, also add a summary notification and call setGroupSummary() on the summary notification. This notification does not appear in your stack of notifications on the wearable, but it appears as the only notification on the handheld device."

    https://developer.android.com/training/wearables/notifications/stacks.html