gpt4 book ai didi

android - 如何正确地将多个通知堆叠在一起?

转载 作者:行者123 更新时间:2023-11-29 14:15:02 29 4
gpt4 key购买 nike

背景

假设我有一些事件会导致显示新通知。

与其显示全新的通知(具有不同的 ID),不如将它们堆叠在一起,如写的 herehere , 所以你实际上替换了前一个。

堆叠式通知示例:

enter image description here

举个例子(这只是一个例子,所以请不要拘泥于此),让我们以 Gmail 为例。如果您收到一封电子邮件,则会显示一条通知。如果用户现在点击它,它将直接转到消息本身。但是,如果收到更多电子邮件,通知将替换为堆叠通知,单击该通知将转到电子邮件列表 Activity 。不仅如此,通知还会显示新电子邮件的数量。

问题

一旦显示通知,您就无法控制那里显示的内容。你只是更换它,你不知道它之前是什么。

问题出现在您希望将多个通知堆叠到一个通知中,并且仍然显示有关先前通知的一些信息。

此外,根据文档,“setDeleteIntent”函数仅在用户明确取消通知时使用:

Supply a PendingIntent to send when the notification is cleared explicitly by the user.

问题

更换新通知时是否可以获取之前的通知数据?

管理通知的推荐方法是什么,以便单个通知具有特定类型和数据(单击时)的 Intent ,而堆叠的通知具有不同的 Intent ?

使用堆叠通知的应用是否真的在其背后管理数据库,并以某种方式监控它们的状态?

最佳答案

您可以访问可见通知。

如第 Updating notifications 段所述,您可以访问由您的应用创建的可见通知,使用相同的通知 ID

mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
...

我还建议将有关未读通知的信息保存在本地 Sqlite 数据库中,并在阅读后将其删除。

关于android - 如何正确地将多个通知堆叠在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996286/

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