Notification pendingIntent contentIntent fails when activity calls finish()

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.


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.

Tags:

Java

Android