gpt4 book ai didi

android - Notification.Builder 中 setGroup() 的用途是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:44 26 4
gpt4 key购买 nike

我在理解 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 Wear 设备。显示远程通知时,您可以将它们组合在一起
  • Android N。运行 Android N 开发者预览版(或 future 官方 N 版本)的设备将同时显示一组通知

以下博文展示了这些在 Android N 上的工作方式:https://medium.com/exploring-android/android-n-introducing-upgraded-notifications-d4dd98a7ca92

下面是一组渲染图:

enter image description here

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

关于android - Notification.Builder 中 setGroup() 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37163258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com