更新android通知将新消息添加到旧消息
我的应用程序发送通知,但问题是,如果操作栏中有一些通知挂起,我想更新消息,将新消息添加到旧消息中 例如,如果消息为Hi!,在用户打开旧消息之前,设备会收到另一条消息(Pamela),我只想显示一条带有消息Hi的通知!帕梅拉 我的发送通知代码是:更新android通知将新消息添加到旧消息,android,notifications,Android,Notifications,我的应用程序发送通知,但问题是,如果操作栏中有一些通知挂起,我想更新消息,将新消息添加到旧消息中 例如,如果消息为Hi!,在用户打开旧消息之前,设备会收到另一条消息(Pamela),我只想显示一条带有消息Hi的通知!帕梅拉 我的发送通知代码是: int icon = R.drawable.ic_launcher; long when = System.currentTimeMillis(); NotificationManager notificationManager
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, msg, when);
String title = this.getString(R.string.app_name);
Intent notificationIntent = new Intent(getApplicationContext(),
FirstActivity.class);
notificationIntent.putExtra("message", msg);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
int notifyID = 1;
PendingIntent intent = PendingIntent.getActivity(this, notifyID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
有可能得到PendingInt的旧信息吗
提前感谢。一种方法是在第一次调用以下内容之前,通过保存已显示的内容来连接字符串:
notificationIntent.putExtra("message", msg);
//save "Hi" , concat "Pamela" update the notification with the same notification id
保存字符串msg
,然后连接在下一个通知中收到的字符串。但是,在这种情况下,需要识别哪些通知包含要连接的字符串部分。您可以为消息设置前缀,也可以通过通知id进行标识
我觉得这不是一种理想的方法,但想不出其他任何方法。无法从NotificationManager检索现有通知。您需要自己独立于通知进行跟踪,并将未读消息保存到其他地方(可能在SQLite数据库或SharedReferences文件中)
您需要确定用户是否刷走了通知,这意味着您必须使用
NotificationCompat.Builder
类来创建通知。您可以使用setDeleteIntent
方法提供在刷走通知时触发的意图,此时,您可以从存储旧邮件的任何位置删除旧邮件,以便新邮件仅显示新邮件的通知。这并不能回答实际问题:>是否可以获取PendingEvent的旧邮件?您是对的,我只是再次阅读问题,编辑我的答案