77

I wrote a simple Android App that show a custom Notification like this:

Context context = getApplicationContext();          
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification( R.drawable.icon, title, System.currentTimeMillis());  
Intent notificationIntent = new Intent( context,  this.getClass()); 
notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE"); 
PendingIntent pendingIntent = PendingIntent.getActivity( context , 0, notificationIntent, 0);               
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(context.getPackageName(), R.layout.notifypbar);
notification.contentIntent = pendingIntent;

notification.contentView.setTextViewText(R.id.notifypb_status_text, text);
notification.contentView.setProgressBar(R.id.notifypb_status_progress, 100, (int)(100*progress), false);

manager.notify(104, notification);

This piece of code is called ONLY ONCE in my application and it displays a notification with a progress bar (all correctly).

Now, when a user clicks on this notification my application handles the onResume event.

public void onResume()
{
    super.onResume();
    // TODO: Extras è SEMPRE NULL!!! impossibile!
    Intent callingintent = getIntent(); 
    Bundle extras = callingintent.getExtras();

but extras is always NULL!

I've tried any combination of:

notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");

or

Bundle extra = new Bundle();
extra.putString(key, value);
notificationIntent.putExtra(extra);

but getIntent().getExtras() returns always NULL.

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
Magius
  • 1,991
  • 2
  • 12
  • 7

5 Answers5

122

This is the scenario:
The method getIntent() returns the FIRST intent than launch activity.

So, when the activity is CLOSED (terminated) and the user clicks on the notification, it will run a new instance of the activity and getIntent() works as expected (Extras is not null).

But if the activity is "sleeping" (it is in the background) and the user clicks on the notification, getIntent() always returns the very FIRST intent that started the activity and NOT the notification intent.

So to catch the notification intent while the application is running, simply use this

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

and then override onNewIntent(Intent newintent).

So when an application first runs, getIntent() can be used and when application is resumed from sleeping, onNewIntent works.

Dave Jensen
  • 4,574
  • 1
  • 40
  • 45
Magius
  • 1,991
  • 2
  • 12
  • 7
  • 44
    The following worked for me: PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); – user672009 Oct 03 '13 at 15:05
  • in my case, add more android:launchMode="singleTask" in launcher activity. Thank you! – Cuong Nguyen Oct 03 '16 at 04:03
101

Just Write this code above your on top of your Resume() method. This is all it takes. This refreshes intent - I don't really know, but it works.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}
coolcool1994
  • 3,704
  • 4
  • 39
  • 43
22

Problem: You are sending the same request code for your pending intens. Change this.

Solution: Set global variable int UNIQUE_INT_PER_CALL =0 and when you create pendingIntent call like below.

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
UNIQUE_INT_PER_CALL++; // to increment.
ucMedia
  • 4,105
  • 4
  • 38
  • 46
Muhammad Mubashir
  • 1,591
  • 1
  • 21
  • 18
4

Since it seems your activity is already running, I think you need to specify FLAG_UPDATE_CURRENT, otherwise the getIntent() call will return the previous one. See this answer.

Community
  • 1
  • 1
dmon
  • 30,048
  • 8
  • 87
  • 96
  • However i had no "PREVIOUS ONE" intent in my application. That piece of code will be called only ONCE and no other intent will be created or updated after that. – Magius Jun 15 '11 at 10:33
  • All activities are started by intents, so there *has* to be a previous intent. Hah, I just saw your own answer, that is what I meant! – dmon Jun 15 '11 at 12:12
  • 1
    This is what solved it for me. here's where to set that flag: http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT – finiteloop Sep 26 '12 at 03:52
-10

Look at Shared Preferences for passing and retrieving persistent key/value pairs.

apesa
  • 12,163
  • 6
  • 38
  • 43
  • i do not need to store data into preferences. I use shared preferences for other purpose. I need to pass ExtraData througth Intent. – Magius Jun 15 '11 at 10:31
  • Actually, this is the only answer that worked for me. In my case, the intent is started from url using activity:scheme, I tried everything, including `savedInstanceState` without luck. – Dohab Mar 21 '21 at 01:11