This seem to be a common problem and I went through all the related questions I could find already:
Activity isn't picking up new intent, Why extra data (integer) is not sent in android notification intent?, Notification passes old Intent Extras, Can't put extras for an intent in notification, android pending intent notification problem; but still cannot figure this out.
Problem is the same. I set a notification with a PendingIntent carrying some extra information and I don't get it on the other side.
Here is the code for generating the notification:
Notification notification = new Notification(R.drawable.icon, getResources().getString(R.string.notification_ticker), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
Intent start_test = new Intent(this, MyActivity.class);
start_test.putExtra("start_test", true);
start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), start_test, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, getResources().getString(R.string.notification_title), getResources().getString(R.string.notification_content, expired), pi);
nm.notify(NOTIFICATION_ID, notification);
And on the other side:
boolean start_test=getIntent().getBooleanExtra("start_test", false);
The bundle is actually not there (getExtras() returns null).
I tried the different flags for PendingIntent.getActivity
(FLAG_UPDATE_CURRENT
, FLAG_CANCEL_CURRENT
, FLAG_ONE_SHOT
), none of those helped.
As shown in the code, I use getCurrentMillis to make sure the requestID changes....
Also found that in MyActivity, onCreate
and onNewIntent
are not getting called. only onResume
is. Even though the FLAG_ACTIVITY_NEW_TASK
is set... ?
Am I missing something very simple ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…