【问题标题】:FCM - How to traverse to a particular activity when system tray notification is clickedFCM - 单击系统托盘通知时如何遍历特定活动
【发布时间】:2017-10-09 14:00:28
【问题描述】:

我们正在使用 FCM 向最终用户显示推送通知。 FCM 成功发出通知,用户正在从系统托盘中看到通知。我正在使用 FCM 控制台通知编写器进行测试。

我会将我的应用程序保持在后台并从 FCM 控制台发送消息。

要求是当用户单击托盘消息时(当应用程序在后台时)导航到应用程序中的特定活动。现在,默认情况下它会打开应用启动器页面。

我无法从 google 得到正确的答案。大多数答案都是针对 GCM 的,大多数都不是有效的解决方案。我在 FCM 页面中找不到合适的文档。

在后台应用中处理通知消息

当您的应用处于后台时,Android 会将通知消息定向到系统托盘。默认情况下,用户点击通知会打开应用启动器。

FCM page - 

这包括同时包含通知和数据的消息 有效负载(以及从通知控制台发送的所有消息)。在 在这些情况下,通知将传递到设备的系统 托盘,并且数据有效负载以附加的意图交付 你的启动器活动。

谢谢

【问题讨论】:

    标签: android firebase-cloud-messaging android-push-notification


    【解决方案1】:

    您必须在 AndroidManifest.xml 中添加带有操作的<intent-filter> 并在通知负载中设置click_action,然后它会在任一应用程序处于后台或被终止时工作。

    您可以参考以下链接:

    open a specific activity from firebase notification

    【讨论】:

      【解决方案2】:

      将此代码放入 onMessageReceived() 方法中

       private void sendNotification(String messageBody) {
                  Intent intent = new Intent(this, YOUR_TARGET_ACTIVITY.class);
                  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                          PendingIntent.FLAG_ONE_SHOT);
      
                  Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                          .setSmallIcon(R.drawable.ic_stat_ic_notification)
                          .setContentTitle("FCM Message")
                          .setContentText(messageBody)
                          .setAutoCancel(true)
                          .setSound(defaultSoundUri)
                          .setContentIntent(pendingIntent);
      
                  NotificationManager notificationManager =
                          (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
                  notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
              }
      

      【讨论】:

      • 当应用在后台时,onMessageReceived() 不会被命中。
      • 将通知设置为高优先级,然后您将在应用处于后台时收到通知。
      【解决方案3】:

      你需要自己处理推送,不要让谷歌自动添加状态通知。默认情况下,Firebase 控制台将为您接管状态通知的传递,并且不允许发生其他任何事情。在您的 Firebase 接收器中,只需根据推送中的数据模式(您设置的)build your own notification,然后在通知中添加 PendingIntent

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多