Notification.Builder中的setGroup()的用途是什么? [英] What the purpose of setGroup() in Notification.Builder?

查看:555
本文介绍了Notification.Builder中的setGroup()的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解 setGroup() 方法.

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

如文档所述:

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

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

这是第一个问题:

我创建了一种显示自定义短信的方法:

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

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

但是,没有视觉变化 不管我是否发表评论.因此,对我来说,了解这种方法的目的是很困难的.

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.

推荐答案

来自官方文档:

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

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

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仅在设备支持的情况下才会有所不同.

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

支持该功能的设备为:

  • Android Wear设备.显示远程通知时,您可以将它们分组在一起
  • AndroidN.运行Android N开发人员预览版的设备(或将来的正式N版本)将一起显示一组通知

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

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

bellow是一组外观的渲染:

bellow is a render of that a group looks like:

这意味着setGroup对于运行以下API23的设备(包括棉花糖,棒棒糖,奇巧等)不会产生任何影响.

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

这篇关于Notification.Builder中的setGroup()的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆