Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the purpose of setGroup() in Notification.Builder?

I have a some troubles with understanding of goal of setGroup() method.

As docs said :

...Grouped notifications may display in a cluster or stack on devices which support such rendering....

Here it is the first question :

What it is this rendering? What's so special about it?!

I create a method which show a custom text message :

    public static void showNotification(Context context, String title, String message, PendingIntent pendingIntent) {
        notificationMessages.add(message);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
//                .setGroupSummary(true)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentInfo("" + (notificationMessages.size()))
                /*.setGroup(++i + "")*/;

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.setBigContentTitle(title);
        for (int i = 0; i < notificationMessages.size(); i++) {
            inboxStyle.addLine(notificationMessages.get(i));
        }

        builder.setContentIntent(pendingIntent);
        builder.setStyle(inboxStyle);

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = builder.build();
        mNotificationManager.notify(0, notification);
    }

and play with notificationID, setGroup and setGroupSummary methods.

    public static void showNotification(Context context, String title, String message, PendingIntent pendingIntent) {
        notificationMessages.add(message);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
//                .setGroupSummary(true)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentInfo("" + (notificationMessages.size()))
                .setGroup(GROUP_KEY);

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.setBigContentTitle(title);
        for (int i = 0; i < notificationMessages.size(); i++) {
            inboxStyle.addLine(notificationMessages.get(i));
        }

        builder.setContentIntent(pendingIntent);
        builder.setStyle(inboxStyle);

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = builder.build();
        mNotificationManager.notify(new Random().nextInt(3), notification);
    }

But, no visual changes comes if I commented lines or not. So here is a stuck for me in understanding of purpose of this method.

like image 690
Sergey Shustikov Avatar asked May 11 '16 12:05

Sergey Shustikov


People also ask

What is the functionality of notification build ()?

To make this possible, Android N uses the existing NotificationCompat. Builder. setGroup() method. Users can expand each of the notifications, and perform actions such as reply and dismiss on each of the notifications, individually from the notification shade.

What is Channel ID in notification?

ChannelId is a unique String to identify each NotificationChannel and is used in Notification. Builder (line 7) when constructing the Notification object. NotificationChannel settings, except channel name and description text, are immutable after it is submitted to NotificationManager at line 5.

How do I manage multiple notifications on Android?

Set the unique id to let Notification Manager knows this is a another notification instead of same notification. If you use the same unique Id for each notification, the Notification Manager will assume that is same notification and would replace the previous notification.


1 Answers

from the official docs:

http://developer.android.com/preview/features/notification-updates.html

Android N also allows you to bundle similar notifications to appear as a single notification. To make this possible, Android N uses the existing NotificationCompat.Builder.setGroup() method. Users can expand each of the notifications, and perform actions such as reply and dismiss on each of the notifications, individually from the notification shade.

Meaning the setGroup will only make a difference if the device supports it.

Devices that support it are:

  • Android Wear devices. when showing remote notifications, you can group together them
  • Android N. Devices running the Android N developer preview (or in the future the official N release), will show a group of notifications together

The following blog post shows how those work on Android N: https://medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92

bellow is a render of that a group looks like:

enter image description here

That means that setGroup will make no difference on devices running anything bellow API23, that includes, Marshamallow, Lollipop, KitKat, etc.

like image 200
Budius Avatar answered Oct 25 '22 00:10

Budius