更新Android的通知与新生产线正在删除previous线 [英] Update android notification with new line is removing previous lines

查看:96
本文介绍了更新Android的通知与新生产线正在删除previous线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想组通知到的摘要。

I want to group notifications into a summary.

我通过让所有通知单个ID实现这一目标。这样,机器人将无法创建新的通知,但更新现有的(减少code):

I achieve this by having a single ID for all notifications. This way android will not create new notifications but update the existing one (reduced code):

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)消失。不过,我想在不具有某种通知管理服务器端添加了新的消息。

This cause the old addLine(msg) to disappear. However I want the new message to be added without having some kind of notification manager server side.

任何意见或建议?

推荐答案

我觉得你misinter preting通知制造商。

I think you are misinterpreting the notification builder.

在NotificationCompat.Builder构建的完全的通知,所有的内容。 重复使用相同的ID只是告诉通知经理更换具有相同id用新现有通知:(的来源

The NotificationCompat.Builder builds the complete notification, with all the content. Reusing the same id simply tells the notification manager to replace an existing notification with the same id with the new one: (Source)

[...]更新或创建一个NotificationCompat.Builder对象,从它建立一个通知对象,并发出带有您使用previously相同ID的通知。如果previous通知仍然可见,该系统从通知对象的内容更新它的 的。

[...] 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 是不是就是在现有的通知执行的操作,但你创建新的生成器(里面是空的,在那个时候)。

Thus addLine is not an operation that is performed on an existing notification, but on the new builder you created (which is empty at that time).

如果你想添加一行与收件箱风格现有通知,您需要为

If you want to add a line to an existing notification with the inbox style, you will need to either

  • 在保持原有生成器对象,根据需要添加线条和重发相同的ID
  • 的通知
  • 创建一个新的建设者和先加旧线,那么新的。您将需要存储或检索从某处旧线(取决于您的应用程序)。

这篇关于更新Android的通知与新生产线正在删除previous线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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