Intent from notification does not have extras

39,819

Solution 1

Setting FLAG_ACTIVITY_NEW_TASK for the notification Intent will cause the following:

  • If the activity is not already running in a task, a new task will be started and the Intent will be delivered to the activity in onCreate()

  • However, if the activity is already running in a task, that task will be brought to the foreground. That's all. The Intent will not be delivered and onNewIntent() will not be called.

If you want the Intent to actually be delivered you need to specify:

start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);

It makes no difference whether the launchMode of the activity is singleTop or not, you still must specify Intent.FLAG_ACTIVITY_SINGLE_TOP in the Intent.

Note: If the activity is already running in a task and the activity is not on top of the activity stack in that task, the task will be brought to the foreground. That's all. The Intent will not be delivered. The only way to make this happen would be to add Intent.FLAG_ACTIVITY_CLEAR_TOP to the other 2 flags, but this may not be what you want to happen (depends on your specific scenario).

See my (still) open issue on Google code at http://code.google.com/p/android/issues/detail?id=17137

Solution 2

I just added the PendingIntent.FLAG_UPDATE_CURRENT flag to my pending intent, and it started working for me (skipped all flags for Intent).

Example Code:

PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Solution 3

  1. If not needed then avoid setting your activity in manifest as a single task. omit this line or change it to singleTop if you can:

      android:launchMode="singleTask”
    
  2. If you must have a single task then in pending intent set flags to : Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK And PendingIntent.FLAG_CANCEL_CURRENT.

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
    pIntent = PendingIntent.getActivity(context, 0, resultIntent,    PendingIntent.FLAG_CANCEL_CURRENT );
    
  3. If you have a single task - Reset your extras in your activity inside onNewIntent() by setIntent(intent);

    protected void onNewIntent(Intent intent) {
           setIntent(intent);
           ... 
     }
    

Solution 4

You can send extras in intent following way

PendingIntent contentIntent ;
Intent intent = new Intent(this,TestActivity.class);
intent.putExtra("extra","Test");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

stackBuilder.addParentStack(ArticleDetailedActivity.class);

contentIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

To Get Intent extra value in Test Activity class you need to write following code :

Intent intent = getIntent();
 String extra = intent.getStringExtra("extra") ;
Share:
39,819
Matthieu
Author by

Matthieu

There are 10 types of people in the world: those who understand binary, and those who don't. (credits)

Updated on July 09, 2022

Comments

  • Matthieu
    Matthieu almost 2 years

    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 ?

    • Squonk
      Squonk almost 12 years
      If you only want to put a single extra then forget using Bundle. Simply use new_intent.putExtra("start_test", true) then at the other end just use getIntent.getBooleanExtra("start_test", false)
    • Matthieu
      Matthieu almost 12 years
      Yes, I tried that way also, it did not change anything... still not getting it on the other side
    • David Wasser
      David Wasser almost 12 years
      Is your application running (or in the background) when this happens? Is MyActivity on top of the activity stack?
    • Matthieu
      Matthieu almost 12 years
      @DavidWasser, Yes, MyActivity is still running in the background (onDestroy has not been called). Not sure if it was on top of the activity stack or not in that task.
    • seema
      seema about 10 years
      new_intent.putExtra("start_test", true) then at the other end just use getIntent.getBooleanExtra("start_test", false) is not working
    • Muhammad Noman
      Muhammad Noman over 6 years
      Referencing to this solution : stackoverflow.com/questions/1198558/…
  • Matthieu
    Matthieu almost 12 years
    Thanks a lot of the detailed explanation. Also had to set the flag in PendingIntent.getActivity to FLAG_CANCEL_CURRENT otherwise onNewIntent would be called several times. Not exactly sure why, but it seems to work as expected now.
  • Darpan
    Darpan over 9 years
    Because it always takes the firs intent delivered to it, not the one from notification. So you will need to override - @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); } so your intents will be received.
  • Makalele
    Makalele about 9 years
    Thanks, that worked! :) No need to add anything more.
  • leonardkraemer
    leonardkraemer over 8 years
    This should be the accepted answer since it is easier to implement
  • runfaj
    runfaj about 8 years
    The accepted answer didn't work for me on api 16 - however, this did on 16 - 23 perfectly
  • siliconeagle
    siliconeagle almost 8 years
    This is the right answer. an additional note that if the activity is already open then onStart isn't fired again so the intent need to be checked in onResume
  • Anh Duy
    Anh Duy over 7 years
    Thank you very much! It helped me save more time. No need to add anything more.
  • Ian Lovejoy
    Ian Lovejoy over 7 years
    I needed this to get extras passed with a notification on API level 19.
  • Nilesh Deokar
    Nilesh Deokar over 6 years
    In my case it worked well with having FLAG_UPDATE_CURRENT to pending intent
  • Harry .Naeem
    Harry .Naeem over 5 years
    After at least 2-3 hours of search, this saved me. Thank alot
  • Eugene Troyanskii
    Eugene Troyanskii about 2 years
    This is not working code at all.