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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With