7

My activity creates a couple of notifications.

Here's how I'm currently doing it, on different resultIntents:

PendingIntent resultPendingIntent =
  PendingIntent.getActivity(
    context,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
  );

Now, since a flag is compulsory, I'm forced to select from the four flags. What do I do if I want all of them to work independently, and the newer notification isn't affected by the previous one.

2 Answers 2

10

A solution was found here: here.

You've to use setAction on the intent to a unique value so that there will be no matching PendingIntents

Here's what I used:

setAction(Long.toString(System.currentTimeMillis()))
3
  • 2
    This makes no sense. If you are always setting the ACTION to a unique value, setting PendingIntent.FLAG_UPDATE_CURRENT is useless, since there will never be a matching PendingIntent. Setting a unique ACTION string guarantees that the call to PendingIntent.getActivity() will always return a new PendingIntent. Jan 26, 2015 at 17:39
  • Yes, that's what I actually required. I was looking for a way to never have a matching PendingIndent. The answer has the flag just because I quoted it from another stackoverflow answer. Sorry if it caused any confusion.
    – Sid Verma
    Jan 28, 2015 at 0:41
  • 1
    You can edit your answer and remove the flag. As I said, the flag makes no sense and is likely to confuse other people. Jan 28, 2015 at 4:46
4

From the official documentation

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

Intent.filterEquals documentation:

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

If you need an unlimited amount of PendingIntents, I would recommend using Intent.setData to some sort of unique value. If you can group them, use setType or addCategory to reduce the number of unique PendingIntents needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.