What the purpose of setGroup() in Notification.Bui

2019-03-22 04:51发布

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.

2条回答
Anthone
2楼-- · 2019-03-22 05:27

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.

查看更多
欢心
3楼-- · 2019-03-22 05:45

setGroupSummaryis there to support grouped notifications on devices before Nougat. It replaces the single notifications with 1 summary notification. On Nougat+ the system creates a normal group, which is not your notification with setGroupSummary(true)

查看更多
登录 后发表回答