android pending intent notification problem

AndroidNotificationsAndroid Intent

Android Problem Overview


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!

Android Solutions


Solution 1 - Android

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();

Solution 2 - Android

> 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 3 - Android

I had this issue in my app and just generated a random number to over come overriding notifications intent:

int random= new Random().nextInt();
PendingIntent resultPendingIntent =
      stackBuilder.getPendingIntent(
              random,
              PendingIntent.FLAG_UPDATE_CURRENT
      );

Solution 4 - Android

I followed the solution provided by U-ramos and this worked for me

int requestID = (int) System.currentTimeMillis();

PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, showIntent, 0);

Solution 5 - Android

another solution:

use the PendingIntent.FLAG_UPDATE_CURRENT like this:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,i, PendingIntent.FLAG_UPDATE_CURRENT);

this worked for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionspagiView Question on Stackoverflow
Solution 1 - Androidu-ramosView Answer on Stackoverflow
Solution 2 - AndroidCommonsWareView Answer on Stackoverflow
Solution 3 - AndroidbshView Answer on Stackoverflow
Solution 4 - Androiddamilare_stack_officialView Answer on Stackoverflow
Solution 5 - AndroidAbeer SulView Answer on Stackoverflow