1

Я показываю уведомление с помощью FCM в своем приложении для Android. Все работает как ожидалось, кроме одного вопроса.Очистить весь стек при нажатии на уведомление

Когда пользователь нажимает на уведомление, мне нужно закрыть все фоновые и передние действия моего приложения и открыть намерение, указанное в уведомлении.

Как я могу это сделать?

Я использую ожидающий намерения как этот

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

ответ

2

Вы можете использовать 2 флаги, чтобы очистить все стеки

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

Try это,

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);   
Смежные вопросы