Android通知栏中启动的待处理意图无法替换上一个。

11

我阅读了许多关于同一主题的帖子,并尝试了所有给出的解决方案,但没有得到我想要的结果。该程序应从通知中使用额外参数启动一个意图:

NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  

Intent notificationIntent = new Intent(context, myActivity.class);
    notificationIntent.putExtra("someData", data);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

mNotificationManager.notify(ID, notification);

问题在于当显示新通知时,添加到意图的额外信息与第一条通知中的相同。我已经尝试使用不同的标志,包括在意图和挂起的意图中,但没有结果。我错在哪里? 如果我用按钮启动相同的活动(和相同的额外信息),则一切都按照预期工作。


1
是的,问题是,我看了那篇帖子和其他几篇,但都不适用于我。然而,以某种方式,我设法解决了它,并很快在这里发布我的解决方案。 - Emil
3个回答

10

我不知道为什么我一直在努力让它工作,但使用以下标志的组合可以使其正常工作:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

我还删除了创建 notificationIntent 时使用的所有标志。


5
尝试在 AndroidManifest.xml 文件中添加一个属性:
<activity ... android:launchMode="singleTop"/>

0
尝试为每个PendingIntent设置请求代码,它就会起作用。
PendingIntent pendingIntent = PendingIntent.getActivity(this, RandomInt, intent,
                PendingIntent.FLAG_ONE_SHOT);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接