This questions somehow relates to the question when I was looking to get the extras back in startActivityForResult but now I face another challenge.
I have subscribed to receive ProximityAlerts and I have explicitly constructed the Intent to include some Extras. But when I got the service the extras are not there.
After the answers here is the working code:
Intent intent = new Intent(this, PlacesProximityHandlerService.class);
intent.setAction("PlacesProximityHandlerService");
intent.putExtra("lat", objPlace.getLat());
intent.putExtra("lon", objPlace.getLon());
intent.putExtra("error_m", objPlace.getError()+ALERT_RANGE_IN_METERS);
PendingIntent sender=PendingIntent.getService(this, 0, intent, 0);
LocationUtils.addProximity(this, objPlace.getLat(), objPlace.getLon(),objPlace.getError()+ALERT_RANGE_IN_METERS, -1, sender);
The documentation says param PendingIntent to be sent for each location update
If you have multiple outstanding
PendingIntents
, you need to make sure that the underlyingIntents
differ on more than their extras. Otherwise, Android will keep reusing the firstPendingIntent
you created for your firstIntent
, using that firstIntent
's extras all of the time.For example, you could add a unique action via
setAction()
-- that will not change yourIntent
routing (since you are specifying the component), but it will make yourIntents
different.The key is to set the extras and the unique action into the intent before calling
if you set the extras and action into the intent after calling the above, it won't work. This will not work:
None of the answers worked for me. Setting action to a specific string works for the first time but if you use the same notification with different extras at a later time, it would not work. I replaced the string for the
setAction
method with a randomly generated one and it works without any issues:nextInt()
For some unspecified reason, extras will be delivered only if you've set some action, for example setAction("foo"). What CommonsWare refers to applies only when obtaining PendingIntent instances, if you haven't set FLAG_ONE_SHOT. That can be fixed by the requestCode argument in PendingIntent.get... factory methods. Although documentation says it's currently not used, it actually takes into count when distinguishing PendingIntents.
In your case, you don't need to set anything else than some dummy action string. LocationManagerService reuses the PendingIntent you have subscribed for proximity alerts, and only adds a flag if phone has entered or exited the alarm range.
I had this problem and the solution I found was quite simple, though I can't explain why it worked.
Initially my pending intent looked like this:
When creating the intent like this, no extras would be passed when the notification was clicked, the extras map would be empty in the receiving activity. I made the following change to the line initializing the notificationIntent:
Now the extras are populated in the receiving activity. Again, I can't explain why this works but it fixed my problem.