Andro Selva
Andro Selva

Reputation: 54322

Restart Application on Notification Received

I have developed an application which has the functionality of receiving notifications from server.

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

This behavior is ok, if my app is not in the foreground, but if it is and I try to open a notification, a new instance of my app is created and thus overlapping the previously opened instance of the app.

I don't want this to happen, so when I click on the notification if my app is in the foreground I have to close that and open a new instance.

How should I override the notification's click event?

Upvotes: 4

Views: 4635

Answers (4)

Farina Ali
Farina Ali

Reputation: 11

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

In this way you will clear your last instance along with all the activities and launch a new instance of the application.

Upvotes: -1

Balaji.K
Balaji.K

Reputation: 4829

you need to do some magic with the IntentFlags. try to add different flags to your intent.

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 5

Vineet Shukla
Vineet Shukla

Reputation: 24031

you can add flag in intent while setting the intent to pending intent like this:

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

Upvotes: 1

Salw
Salw

Reputation: 1900

If you use action MAIN and category LAUNCHER in your intent, it will resume your existing instance. It is same as you launch your aplication from launcher or last used applications. Probably category LAUNCHER is not necessary.

Upvotes: 0

Related Questions