1

我在Android應用上顯示使用FCM的通知。除了一個問題,一切都按預期工作。點擊通知時清除所有堆棧

當用戶點擊通知時,我需要關閉我的應用程序的所有後臺和前臺活動,並打開通知中指定的意圖。

我該怎麼辦?

我用這樣的

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

試試這個,

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);