【问题标题】:Clear all stack when notification is clicked单击通知时清除所有堆栈
【发布时间】:2017-02-20 06:03:18
【问题描述】:

我正在我的 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
                );

【问题讨论】:

    标签: android firebase android-notifications firebase-cloud-messaging


    【解决方案1】:

    你可以使用 2 个标志来清除所有堆栈

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

    【讨论】:

      【解决方案2】:

      试试这个,

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-26
        • 1970-01-01
        相关资源
        最近更新 更多