Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification pendingIntent contentIntent fails when activity calls finish()

Tags:

java

android

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.

like image 657
brandall Avatar asked Sep 20 '12 08:09

brandall


1 Answers

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.

like image 74
Sreedevi J Avatar answered Nov 15 '22 18:11

Sreedevi J