Restart notification request

I developed an application that has the function of receiving notifications from the server.

The problem is that when I click on the notification I received, it opens a new instance of my application itself .

This is normal if my application is not in the foreground, but if it is, and I try to open a notification, a new instance of my application is created and, thus, overlaps a previously opened instance of the application.

I do not want this to happen, so when I click on the notification, if my application is in the foreground , I have to close it and open a new instance .

How do I override the notification click event?

+4
source share
4 answers

you need to do the magic with IntentFlags. try adding different flags to your plan.

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK); 
+5
source

you can add a flag to an intent by specifying the intent to the pending intent as follows:

 Intent notificationIntent = new Intent(context, activity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
+1
source

If you use the MAIN action and the LAUNCHER category in your intent, it will renew your existing instance. This is the same way you launch an application from a launch or the most recently used applications. Probably the LAUNCHER category is not needed.

0
source
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 

This way you clear your last instance along with all the actions and launch a new instance of the application.

-1
source

All Articles