android pending intent notification problem
Solution 1
The problem is that when I create more than one alarm then the activity launched from the notification gets the same extras as the first one.
Correct.
How can I make it to launch the correct intents?
That depends on whether you have two alarms that will be registered at once, or not.
If not, you can use FLAG_ONE_SHOT
or one of the other PendingIntent
flags to have your second PendingIntent
use the newer extras.
If, however, you will have two alarms registered at once, with different Intent
extras, you will need to make the two Intents
be more materially different, such that filterEquals()
returns false
when comparing the two. For example, you could call setData()
or setAction()
and provide different values for each Intent
.
Solution 2
The way I solved that problem was by assigning a unique requestCode when you get the PendingIntent:
PendingIntent.getActivity(context, requestCode, showIntent, 0);
By doing so you are registering with the system different/unique intent instances. Tip: A good way of making the requestCode unique would be by passing to it the current system time.
int requestID = (int) System.currentTimeMillis();
spagi
Updated on July 05, 2022Comments
-
spagi almost 2 years
I have a alarm thing going on in my app and it launches a notification that then when pressed launched an activity. The problem is that when I create more than one alarm then the activity launched from the notification gets the same extras as the first one. I think the problem is either with the intent i put in the pending intent or in the pending intent itself. I think I might need to put a flag on one of these but I dont know which one.
Intent showIntent =new Intent(context, notificationreceiver.class); showIntent.putExtra("details", alarmname); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, showIntent, 0); notification.setLatestEventInfo(context, "The event is imminent", alarmname, contentIntent);
And the receiver of the notification
Bundle b = getIntent().getExtras(); String eventname = b.getString("details"); details.setText(eventname);
The "details" extra is the same to every the next time a notification happens instead of having the different value. Until I set the intents I am sure that the correct value goes to the "details" so its a problem of getting the first intent everytime i press any notification. How can I make it to launch the correct intents? Hope I was as clear as i could Thanks!
-
spagi almost 14 yearsMan thanks alot for the help. Because there is the possibility to have alarms going of together and having more than one notification on the notification bar I used the FLAG_UPDATE_CURRENT flag for the pending intent. But the real deal was as you said to setAction on the intent with the alarm name and now it could know which intents are different and what the same. Thanks again!
-
Roman Royter over 13 yearssetAction worked for me too. I just wonder why the official SDK doesn't mention it.
-
Soham over 12 yearssetAction works, thanks a TON. Using setAction helped me differentiate all the pendingIntents, just awesome!
-
Kitteh over 12 yearsrequestCode is indeed not used by anything as of yet, but it does make the intents unique which stops them being overwritten! Excellent work u-ramos!
-
Christian García over 11 yearsusing setAction to set different Action for each content Intent was key for me. Thanks a LOT!
-
Snicolas almost 11 yearsYou can also use a different request code when creating pending intents, with a hash, that's quite convenient, and maybe cleaner than using setData
-
Abdullah Shoaib over 10 yearsEven if
TaskStackBuilder
is being used, we have a method theregetPendingIntent(int requestCode, int flags)
.Here this requestID can be passed to. -
djpeinado over 10 yearsBe careful, currentTimeMillis is type long, so if you cast to int, it will be truncated. I don't know how probable is and if it matters, but it could be repeated. In that case you can use a static incremented value
-
Bibaswann Bandyopadhyay over 8 yearsThis doesn't work. It replaces the old parameters and replaces with new ones
-
Bibaswann Bandyopadhyay over 8 yearsGreat solution. Today I came to know what requestCode is for
-
Amal Kronz over 8 yearsthanks alot :),i make app that give more than one notification at different time and make different action to each notification so faced this problem when put extra data but it solved just by using Intent intent1=new Intent(getBaseContext(),Splash.class); intent1.putExtra("iqama_ID",6);:PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), notification_id, intent1, PendingIntent.FLAG_ONE_SHOT);
-
ruclip over 8 yearsThank you! Perfect solution!
-
Fivos about 5 yearsWhat if you want later to cancel the
PendingIntent
? You should store the requestCode for eachPendingIntent
? -
AJW about 4 years@CommonsWare I have set up my pendingIntents with unique requestCodes, using an int counter. I am trying to cancel a pendingIntent in Activity A from Activity B here: stackoverflow.com/questions/61161676/… Any ideas on how to fix?