11

I have a foreground service notification that when clicked should start an activity. This activity is very short lived before it calls finish().

The first time the notification is clicked it works, the second time and thereafter I get the error:

    Sending contentIntent failed: android.app.PendingIntent$CanceledException

In my code when creating the foreground service notification, I've changed the randomActivity.class to another Activity class that does not call finish and it works perfectly on every click. From:

    Intent notificationIntent = new Intent(this, RandomActivity.class);

to:

    Intent notificationIntent = new Intent(this, HomeActivity.class);

Works fine...

I've used the standard notification code from the Android Developers website, as well as testing it using Notification builder. I get the same result regardless. It works perfectly unless the Activity calls finish();

Is this expected behaviour, a bug, or am I missing something?

I thank you in advance for you help and hopefully a solution!

Note: The notification code I use is completely standard, so I haven't posted it. RandomActivity calls finish(); in onCreate, so there's nothing unusual to see there either.

4 Answers 4

12

After trying everything I possibly could, I eventually found a solution. Posting in case anyone stumbles across this issue too.

I had to match the int requestCode to the notification id. Why? Absolutely no idea... I can only assume it prevents the intent data from becoming null or reusing it?

    private static int ONGOING_NOTIFICATION_ID = 76;

    PendingIntent contentIntent = PendingIntent.getActivity(this,
    ONGOING_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

The same as the notification id to startForeground:

    this.startForeground(ONGOING_NOTIFICATION_ID, not);

Hope this helps someone.

1
  • I agree with @brandall. If you are using action buttons, the notificationID should still use the requestCode from the contentIntent and not any of the action intents.
    – fawaad
    Commented Jan 19, 2017 at 20:47
7

I didn't need to check the notification id (like you suggested), but I did have to change the flag to FLAG_UPDATE_CURRENT, rather than FLAG_ONE_SHOT.

With FLAG_ONE_SHOT, the pending intent gets canceled once delivered, and after that, no amount of tapping the notification will allow the same pending intent to be delivered again, hence the exception.

This was the problem for me.

1

I've just had a similar problem, it was not a foreground notification, but sometimes clicking the notification caused the Sending contentIntent failed: android.app.PendingIntent$CanceledException error and nothing happened. So I just want to describe my issue and solution, because I was looking for it here.

For me the problem was that I've had one leftover installation object on server, so I was actually getting the FCM onMessageReceived event twice and publishing the local notification twice. It was implemented to replace itself, because it was related to the same event, so I did not notice.

But I was using current time for generating a unique requestCode, with 10ms precision, so if the second call published the local notification in 10ms after the first call, it got the same request code.

I was also using flag FLAG_CANCEL_CURRENT, but it should just cancel the previous pending intent and create a new one.

So, then I tried to make the requestCode a constant - still, sometimes the intent was working fine.

I think the culprit was that the two calls were running paralelly and sometimes, the notification that got its pendingIntent cancelled, was actually published later, replacing the one which had the working pendingIntent.

My solution: I ensured that the code in onMessageReceived waits for when the previous call completes. It stopped happening and everything works fine. I also added some filtering inside the onMessageReceived method to ignore incoming pushes which have the exact same data and are received in less then 5 seconds apart.

0

My issue stemmed from multiple PendingIntents being equal; when this is the case, cancelling one PendingIntent cancels all "duplicates".

Per the Android documentation:

it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent#filterEquals(Intent). If you use two Intent objects that are equivalent as per Intent#filterEquals(Intent), then you will get the same PendingIntent for both of them.

Two suggestions on how to deal with this issue:

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(Intent), 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).

If you only need one PendingIntent active at a time for any of the Intents you will use, then you can alternatively use the flags FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT to either cancel or modify whatever current PendingIntent is associated with the Intent you are supplying.

Two intents are equal when:

their action, data, type, identity, class, and categories are the same. This does not compare any extra data included in the intents. Note that technically when actually matching against an IntentFilter the identifier is ignored, while here it is directly compared for equality like the other fields.

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.