在Notification.Builder中,setGroup()方法的目的是什么?

6

我在理解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);
    }

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

但是,无论我是否注释掉这些代码,都没有任何视觉变化。所以,对于我来说,这个方法的目的还很困惑。


这只适用于Android N。你使用的是什么设备? - Budius
@Budius 5.0 棒棒糖。Nexus 5X - Sergey Shustikov
@NicolasSimon 不,那不是真的。如果我将notificationID更改为不同的数字,我将有三个单独的通知,但它们不会折叠成一组。(我说的是视觉效果)。 - Sergey Shustikov
@Budius 但是这篇文章与N版本有关,而不是早期版本。我在NexusX 5.0上安装了一个版本,但不是最新的。 - Sergey Shustikov
我已经添加了一个适当的答案来解释它。 - Budius
显示剩余3条评论
2个回答

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

以下是组的渲染效果:

enter image description here

这意味着在运行低于API23的设备上,包括Marshmallow、Lollipop、KitKat等,setGroup不会产生任何影响。


1
我按照同样的教程操作,为什么第一个通知没有显示出来呢?https://dev59.com/Hpjga4cB1Zd3GeqPKnmO - Srinivasan
这意味着在运行API23以下的设备上,包括Marshmallow、Lollipop、KitKat等,setGroup将不会产生任何影响。在4.4.2上,如果调用setGroup,则根本不会显示通知。只是想指出这一点,以便其他人不要浪费时间进行调试。 - Dan Neacșu
1
这个与编程有关的内容翻译成中文:这个链接的示例是不正确的。它在所有通知上执行.setGroupSummary(true),它应该只在第一个上执行,也就是下拉的领导者。 - FirefighterBlu3

0

setGroupSummary用于支持Nougat之前的设备上的分组通知。它将单个通知替换为1个摘要通知。 在Nougat及以上版本中,系统会创建一个普通的组,这不是您使用setGroupSummary(true)设置的通知。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接