0

I am receiving some json messages with C2DM, so far this is good. Having the json message extracted from the C2DM payload, I create a notification that upon user click will open an activity that will display the message received.

The first time the C2DM message is received (for example, "test number 1" message), the notification is created and the activity starts successfully when the user clicks it and I can see the message "test number 1". Then I send a second C2DM message, with the text "test number 2", the notification is created but when I click in the notification, the activity starts I see "test number 1" message, instead of the second message.

I am creating my notification like this:

  public static void createMessageNotification(Context context, Message msg) {
     int icon = R.drawable.ic_stat_notify_msg;          // icon from resources
     CharSequence tickerText = "You've got a new message";  // ticker-text
     long when = System.currentTimeMillis();            // notification time
     CharSequence contentTitle = "Service Message";  // message title
     CharSequence contentText = "New message";

     Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
     notificationIntent.setClass(context, MessageDetailsActivity.class);
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
     Bundle b = new Bundle();
     b.putSerializable("message", msg);
     notificationIntent.putExtras(b);

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

     Notification notification = new Notification(icon, tickerText, when);
     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
     long[] vibrate = {0,100,200,300};
     notification.vibrate = vibrate;
     notification.flags = Notification.FLAG_AUTO_CANCEL;

     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
     mNotificationManager.notify(AppUtil.getNextPushIndexMessage(), notification);
  }

MessageDetailsActivity.java can be found here: http://pastebin.com/tmBK7rNH

I see in the logs that the message is coming from the C2DM service correctly, with new data and values, but I cant make the MessageDetailsActivity to display the new information.

Thank you T

Thiago
  • 5,152
  • 11
  • 35
  • 44

1 Answers1

2

Calling PendingIntent.getActivity() the second time is probably returning the same PendingIntent from when you called it the first time.

This can be somewhat confusing. Android keeps a cache of PendingIntents and when you call getActivity() or getBroadcast() or getService() it tries to find a cached PendingIntent that matches the parameters you provide to return to you. You've given an Intent to match against in your call to getActivity() and the only difference between the Intent you gave it the first time and the Intent you gave it the second time is in the Extras. Unfortunately, when you call getActivity() it ignores the Extras when it tries to find a matching PendingIntent in its cache, so it returns the first one you created.

To get around this, change your call to getActivity() to include the FLAG_UPDATE_CURRENT flag, like this:

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

This will replace the data of the cached PendingIntent to include the new Extras.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • @DavidWasser thank you for your answer, it works pretty well. But I have one question: If I get two notifications, no matter which notifications the user clicks, with your approach, the activity will get the extras from the second notification, even if the first notification was clicked. Is there a workaround for this? In my case my notifications are about messages that the user received, so if the clicks the first notification I should display the message for the notification, instead of displaying the last message that arrived. Thanks a lot – Thiago May 30 '12 at 01:56
  • Ah, I wasn't sure exactly what you were trying to do. If you want to have 2 separate notifications, each of which have different data, you need to make sure that a new `PendingIntent` gets created each time you call `getActivity()`. To do this, remove the FLAG_UPDATE_CURRENT and set the second parameter (requestCode) of `getActivity()` to a unique integer each time. This will ensure that a new `PendingIntent` is created every time you call `getActivity()`. – David Wasser May 30 '12 at 07:54