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