Notification pendingIntent contentIntent fails when activity () call completes

I have a notification about the maintenance of the foreground, which when clicked should start with the action. This operation is very short before it calls finish ().

The first time the notification is clicked, it works, the second time and after that I get an error message:

Sending contentIntent failed: android.app.PendingIntent$CanceledException 

In my code, when creating a front-end maintenance notification, I changed randomActivity.class to another Activity class that does not cause termination, and it works great every time I click. From:

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

in

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

Works great ...

I used the standard notification code on the Android Developers website and also tested it with the Notification builder. I get the same result regardless. It works fine if Activity calls finish ();

Is this expected behavior, a mistake, or am I missing something?

I thank you in advance for your help and, I hope, a solution!

Note. The notification code that I use is completely standard, so I did not post it. RandomActivity calls finish (); in onCreate, so nothing unusual is visible there.

+6
source share
2 answers

After I tried everything I could, I found a solution. Posting in case someone also encounters this problem.

I had to map int requestCode to notification id. What for? I absolutely do not know ... I can only assume that these intentions do not become zero or are reused?

  private static int ONGOING_NOTIFICATION_ID = 76; PendingIntent contentIntent = PendingIntent.getActivity(this, ONGOING_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

Same as notification id to start foreground:

  this.startForeground(ONGOING_NOTIFICATION_ID, not); 

Hope this helps someone.

+10
source

I did not need to check the notification identifier (for example, you suggested), but I had to change the flag to FLAG_UPDATE_CURRENT , and not to FLAG_ONE_SHOT .

With FLAG_ONE_SHOT expected intention will be canceled after it is delivered, and after that no click on the notification will allow the same pending intention to be repeated as the exception.

This was a problem for me.

+2
source

Source: https://habr.com/ru/post/925824/


All Articles