Reputation: 12067
When I create a notification that is sent through C2DM and received in my app I want to pass some of the data that came with the push notification from C2DM in the intent extras. This works fine the first time I open my notification. Then the data is received with onNewIntent or in onCreate depending on the state of the activity.
But if I send a second push notification through C2DM it is received correctly with the new data but when getting the extras from the intent I still get the data from the previous message. The title and the data I want to see is shown correctly in the notification. So something must be wrong the my intent. This happens if the activity is running and if it isn't.
To create the notification I do the following:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_stat_notify_push, "Message received", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.defaults |= Notification.DEFAULT_LIGHTS;
Intent intent = new Intent(context, DesktopApp.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("msg_id", msg_id);
intent.putExtra("title", title);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, "New message", title + String.valueOf(msg_id), pendingIntent);
notificationManager.notify(0, notification);
To then read the intents:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myIntent = getIntent(); // this is just for example purpose
int i = myIntent.getIntExtra("msg_id", -1);
if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show();
}
@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
Bundle extras = intent.getExtras();
if (extras != null) {
int i = extras.getInt("msg_id", -1);
if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show();
}
}
Any suggestions?
Upvotes: 19
Views: 8336
Reputation: 129
Use this
PendingIntent pi = PendingIntent.getActivity(context, UNIQUE_ID,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
Upvotes: 5
Reputation: 30994
You need to set up a clean flag to PendingIntent
:
PendingIntent pintent =
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
Have a look at this post that gives a longer explanation.
Upvotes: 37