Eswar
Eswar

Reputation: 172

Clear all stack when notification is clicked

I'm showing a notification using FCM on my android app. Everything is working as expected except one issue.

When user clicks on the notification, I need to close all background and foreground activities of my app and open the intent specified in the notification.

How can I do it?

I'm using pending intent like this

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        final PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        mContext,
                        0,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

Upvotes: 1

Views: 2147

Answers (2)

Komal12
Komal12

Reputation: 3348

Try this,

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

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

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

    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);        

Upvotes: 1

RoShan Shan
RoShan Shan

Reputation: 2954

YOu can use 2 flags to clear all stacks

  intentToLaunch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intentToLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 4

Related Questions