GetIntent () Additionally always NULL

I wrote a simple Android application that shows a custom notification:

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 part of the code is called ONLY ONE in my application and displays a notification with a progress bar (everything is correct).

Now, when the user clicks on this notification, my application is handling the onResume event.

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

but additional functions are always NULL!

I tried any combination:

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

or

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

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

+74
android android-intent notifications
Jun 15 '11 at 2:15
source share
5 answers

This is the scenario:
The getIntent() method returns FIRST than the start.

So, when the action is CLOSED (completed), and the user clicks on the notification, he will start a new instance of the activity, and getIntent() works as expected (Extras is not null ).

But if the action is "sleeping" (it is in the background) and the user clicks on the notification, getIntent() always returns the FIRST intention that launched the action, and NOT the intention of the notification.

So, to catch the intent of the notification 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 the application starts, getIntent() can be used, and when the application resumes from sleep mode, onNewIntent works.

+113
Jun 15 '11 at 11:59
source share
โ€” -

Just write this code above your Resume () method. That is all that is needed. It refreshes the intention - I really don't know, but it works.

 @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); } 
+90
Jan 12 '13 at 0:29
source share

Problem: You are sending the same request code for pending intensity. Change it.

Solution : set the global variable int UNIQUE_INT_PER_CALL = 0 and when creating the pendingIntent call, as shown below.

 PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0); UNIQUE_INT_PER_CALL++; // to increment. 
+15
May 09 '13 at 9:46
source share

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

+3
Jun 15 2018-11-11T00:
source share

See General Settings for transmitting and searching for constant key / value pairs.

-8
Jun 15 '11 at 2:40
source share



All Articles