简体   繁体   English

Android Pending意图从通知开始不会替换最后一个

[英]Android Pending intent started from notification doesn't replace the last

I've read many posts on the same topic and tried all the given solutions without getting the result I want. 我已经阅读了很多关于同一主题的帖子,并尝试了所有给定的解决方案而没有得到我想要的结果。 The program should start an intent with extras from a notification: 程序应该从通知中添加额外的意图:

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);

The problem is that when a new notification is shown, the extras added to the intent is the same as in the first notification. 问题是,当显示新通知时,添加到意图的附加内容与第一个通知中的附加内容相同。 I've triend with differnt flags in both the intent and the pending intent, without result. 我在意图和未决意图中都有不同的标志,没有结果。 What am I getting wrong? 我错了什么? If i just launch the same activity (and the same extras) with a button, everything works as it's supposed to. 如果我只是用一个按钮启动相同的活动(和相同的附加功能),一切都按预期工作。

I don't know why I've had such problems with getting this to work. 我不知道为什么我有这样的问题让这个工作。 The combination of flags I used to get it to work properly was: 我用来使其正常工作的标志组合是:

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

I also removed all of the flags used when creating the notificationIntent . 我还删除了创建notificationIntent时使用的所有标志。

尝试在AndroidManifest.xml文件中添加属性:

<activity ... android:launchMode="singleTop"/>

Try to set request code for each PendingIntent and it will work 尝试为每个PendingIntent设置请求代码,它将起作用

PendingIntent pendingIntent = PendingIntent.getActivity(this, RandomInt, intent,
                PendingIntent.FLAG_ONE_SHOT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM