gpt4 book ai didi

android - 使用新行更新 android 通知正在删除以前的行

转载 作者:搜寻专家 更新时间:2023-11-01 07:58:00 24 4
gpt4 key购买 nike

我想将通知分组到摘要中。

我通过为所有通知设置一个 ID 来实现这一点。这样 android 就不会创建新的通知,而是更新现有的通知(减少代码):

Notification summaryNotification = new NotificationCompat.Builder(this)
.setGroupSummary(true)
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.InboxStyle()
.addLine(msg)
.setBigContentTitle("My App")
.setSummaryText("FooBar"))
.build();

mNotificationManager.notify(uuid, summaryNotification);

UUID 始终相同,因此应更新通知。但是,当新通知到达时,setStyle 似乎被覆盖了。

这会导致旧的 addLine(msg) 消失。但是,我希望在没有某种通知管理器服务器端的情况下添加新消息。

有什么想法或建议吗?

最佳答案

我认为您误解了通知生成器。

NotificationCompat.Builder 构建包含所有内容的完整 通知。重复使用相同的 id 只是告诉通知管理器用新的 id 替换现有的具有相同 id 的通知:( Source )

[...] update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously. If the previous notification is still visible, the system updates it from the contents of the Notification object.

因此 addLine 不是对现有通知执行的操作,而是对您创建的新构建器(当时为空)执行的操作。

如果您想使用收件箱样式向现有通知添加一行,您需要要么

  • 保留原始构建器对象,根据需要添加行并使用相同的 id 重新发送通知
  • 创建一个新的构建器并先添加旧行,然后再添加新行。您将需要从某个地方存储或检索旧行(取决于您的应用程序)。

关于android - 使用新行更新 android 通知正在删除以前的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25137536/

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