Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 884
Emil Avatar asked Jul 05 '10 12:07

Emil


2 Answers

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.

like image 108
Emil Avatar answered Nov 16 '22 13:11

Emil


Try to add an attribute in AndroidManifest.xml file:

<activity ... android:launchMode="singleTop"/>
like image 33
Vaha Avatar answered Nov 16 '22 13:11

Vaha