Notification.Builder 中 setGroup() 的目的是什么?

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

我在理解 setGroup() 方法的目标时遇到了一些麻烦。

正如文档所说:

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

这是第一个问题:

这是什么效果图?它有什么特别之处?!

我创建了一个显示自定义文本消息的方法:

    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);
    }

并使用 notificationIDsetGroupsetGroupSummary 方法。

    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);
    }

但是,视觉上没有变化 如果我评论行与否。所以我在理解这种方法的目的时遇到了困难。

来自官方文档:

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.

意思是 setGroup 只有在设备支持的情况下才会有所不同。

支持它的设备有:

  • Android 穿戴设备。显示远程通知时,您可以将它们组合在一起
  • AndroidN.Devices运行AndroidN开发者预览版(或日后N正式版),会一起展示一组通知

以下博客 post 展示了这些在 Android N: https://medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92

上的工作原理

波纹管是一组看起来像的渲染图:

这意味着 setGroup 对设备 运行 任何低于 API23 的东西都没有影响,包括 Marshamallow、Lollipop、KitKat 等

setGroupSummary是否支持在 Nougat 之前的设备上进行分组通知。它用 1 个摘要通知替换单个通知。 在 Nougat+ 上,系统会创建一个普通组,即 not 您的通知 setGroupSummary(true)