Intent set by NotificationManager is not reflected in activity's onResume()
I have a broadcast receiver that, upon receiving something, will create a pending intent, package it with some data, and use it to create a notification with NotificationManager. The activity that is specified in the pending intent, when NotificationManager resumes the activity, always reads out the original pending intent's data -- it never reads out any new data when set by subsequent onReceive()'s from the broadcast receiver. Here's a snippet from the broadcast receiver:
public void onReceive( Context context, Intent intent ) {
String action = intent.getAction();
if( action.equals( "someaction" ) ) {
long now = System.currentTimeMillis();
int icon = R.drawable.icon;
CharSequence tickerText = context.getString( R.string.tickerText );
CharSequence contentTitle = context.getString( R.string.contentTitle );
CharSequence contentText = context.getString( R.string.contentText );
Intent notificationIntent = new Intent( context, MyActivity.class );
Log.d( TAG, "Creating notification with data = " + intent.getStringExtra( "somestring" ) );
notificationIntent.putExtra( "somestring", intent.getStringExtra( "somestring" ) );
notificationIntent.addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP );
notificationIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent contentIntent = PendingIntent.getActivity( context, 0, notificationIntent, 0 );
Notification notification = new Notification( icon, tickerText, now );
NotificationManager notificationmgr = (NotificationManager)context.getSystemService( Context.NOTIFICATION_SERVICE );
notification.flags |= Notification.FLAG_AUTO_CANCEL |
Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE;
notification.setLatestEventInfo( context, contentTitle, contentText, contentIntent );
notificationmgr.cancelAll();
notificationmgr.notify( 0, notification );
}
}
and here's that of the activity:
protected void onStart() {
super.onStart();
Intent intent = getIntent();
String somestring = intent.getStringExtra( "somestring" );
Log.d( TAG, "onStart(), somestring = " + somestring );
}
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String somestring = intent.getStringExtra( "somestring" );
Log.d( TAG, "onResume(), somestring = " + somestring );
}
protected void onNewIntent( Intent intent ) {
Log.d( TAG, "onNewIntent(), intent = " + intent );
super.onNewIntent( intent );
setIntent( intent );
}
So here's the scenario: From the home screen, my receiver receives some intent with, say, somestring set to "hello world". The notification bar shows the notification, I slide down the notification bar, and tap on the notification to launch (create or resume) the activity. The activity reads out "hello worl开发者_运维百科d" correctly. I leave the activity in the foreground or background it with the home key. The second time around my broadcast receiver receives, say, "hello again" as somestring. It again creates the notification, this time with "hello again", except when resuming the activity via the notification, I see in the debug that neither getIntent() nor onNewIntent() reflects the updated value of somestring (i.e. "hello again"). That is, it's still holding onto the old value of "hello world".
Any ideas? I can't seem to find a way to force an update of the intent's data. Any help is appreciated; thanks in advance.
Fixed it. When creating the pending intent, one should pass PendingIntent.FLAG_UPDATE_CURRENT
as such:
PendingIntent contentIntent = PendingIntent.getActivity( context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );
I struggled with this for a couple of hours today, you can ALSO set the flag to 0 if you don't need it! I don't really need it, so helped me :)! Hope it helps someone else!
精彩评论