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

Ser*_*kov 6 android android-notifications

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

正如文档所说:

...分组通知可能会显示在支持此类渲染的设备上的集群或堆栈中....

这是第一个问题:

这是什么渲染?它有什么特别之处?!

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

    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);
    }
Run Code Online (Sandbox Code Playgroud)

并使用notificationID,setGroupsetGroupSummary方法。

    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);
    }
Run Code Online (Sandbox Code Playgroud)

但是,无论我是否注释行,都不会出现视觉变化。所以这让我无法理解这种方法的目的。

Bud*_*ius 5

来自官方文档:

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

Android N 还允许您捆绑类似的通知以显示为单个通知。为了实现这一点,Android N 使用了现有的 NotificationCompat.Builder.setGroup() 方法。用户可以展开每个通知,并从通知栏单独对每个通知执行诸如回复和关闭之类的操作。

这意味着setGroup只有在设备支持它时才会有所作为。

支持它的设备有:

  • Android Wear 设备。显示远程通知时,您可以将它们组合在一起
  • Android N. 运行 Android N 开发者预览版(或未来正式发布的 N 版)的设备,将一起显示一组通知

以下博客文章展示了它们在 Android N 上的工作原理:https : //medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92

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

在此处输入图片说明

这意味着这setGroup对运行 API23 以下任何内容的设备没有任何影响,包括 Marshamallow、Lollipop、KitKat 等。