开发者

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 s开发者_开发问答ame 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.


Try to add an attribute in AndroidManifest.xml file:

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


Try to set request code for each PendingIntent and it will work

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜