gpt4 book ai didi

android - 如何对像whatsapp这样的android通知进行分组?因为 setGroup 不适合我

转载 作者:行者123 更新时间:2023-12-04 13:43:38 26 4
gpt4 key购买 nike

我目前正在奥利奥和 Lollipop 设备上进行测试。到目前为止我所做的:

final static String GROUP_KEY_NOTIFY = "group_key_notify";
int notificationId0 = 100;
int notificationId1 = 101;
int notificationId2 = 102;
int notificationId3 = 103;

NotificationCompat.Builder builderSummary =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("A Bundle Example")
.setContentText("You have 3 new messages")
.setGroup(GROUP_KEY_NOTIFY)
.setGroupSummary(true);

NotificationCompat.Builder builder1 =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("New Message")
.setContentText("You have a new message from Kassidy")
.setGroup(GROUP_KEY_NOTIFY);

NotificationCompat.Builder builder2 =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("New Message")
.setContentText("You have a new message from Caitlyn")
.setGroup(GROUP_KEY_NOTIFY);

NotificationCompat.Builder builder3 =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("New Message")
.setContentText("You have a new message from Jason")
.setGroup(GROUP_KEY_NOTIFY);

NotificationManager notifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notifyMgr.notify(notificationId1, builder1.build());
notifyMgr.notify(notificationId2, builder2.build());
notifyMgr.notify(notificationId3, builder3.build());
notifyMgr.notify(notificationId0, builderSummary.build());

我注意到的是,如果发生 4 个或更多通知,那么它们会 bundle 在一起,但是当通知少于 4 个时,它们不会 bundle 在 N 以上的 android 设备中。我阅读了文档并按照他们所说的使用 setGroup 方法并为summaryNotification制作单独的通知对象。但没有什么对我有用。

最佳答案

您可以使用this link for reference for creating bundled notification.

例子:

String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";

Notification newMessageNotification = new
NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.new_mail)
.setContentTitle(emailObject.getSenderName())
.setContentText(emailObject.getSubject())
.setLargeIcon(emailObject.getSenderAvatar())
.setGroup(GROUP_KEY_WORK_EMAIL)
.build();

Notification summaryNotification =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle(emailObject.getSummary())
//set content text to support devices running API level < 24
.setContentText("Two new messages")
.setSmallIcon(R.drawable.ic_notify_summary_status)
//build summary info into InboxStyle template
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex Faarborg Check this out")
.addLine("Jeff Chang Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("janedoe@example.com"))
//specify which group this notification belongs to
.setGroup(GROUP_KEY_WORK_EMAIL)
//set this notification as the summary for the group
.setGroupSummary(true)
.build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(emailNotificationId1, newMessageNotification);
notificationManager.notify(SUMMARY_ID, summaryNotification);

关于android - 如何对像whatsapp这样的android通知进行分组?因为 setGroup 不适合我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52929224/

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