【问题标题】:Android - read data from push notification on open the appAndroid - 在打开应用程序时从推送通知中读取数据
【发布时间】:2019-09-11 13:50:27
【问题描述】:

我已使用 Firebase Cloud Messaging 设置我的应用以接收通知。

我创建了一个从 FirebaseMessagingService 扩展的服务并覆盖了 onMessageReceived 方法。在这里,我创建了一个通知,当您单击它时,根据作为参数提供的 RemoteMessage 数据导航到一个活动。 这不是问题,但它仅在应用程序处于前台或后台时才有效。

当应用程序关闭时,操作系统负责启动通知,问​​题就来了。 当我点击它时,第一个活动被打开(在我的例子中是 SplashActivity),我必须阅读通知数据才能知道接下来要打开什么活动。

我不知道该怎么做。 有没有办法在 SplashActivity 中获取这些数据?

【问题讨论】:

标签: android


【解决方案1】:

可能与this 重复。

简而言之,当应用程序处于后台(或关闭,就此而言)时,通知的data 段将传递给intent extras。

来自谷歌的文档:

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

这包括同时包含通知和数据负载的消息(以及从通知控制台发送的所有消息)。在这些情况下,通知会传送到设备的系统托盘,而数据负载会在启动器 Activity 的 Intent 的附加部分中传送。

因此,在您的SplashActivity 中,您可以执行以下操作:

@Override
public void onCreate(Bundle savedInstanceState) {

    if (getIntent() != null) {
         Bundle dataBundle = getIntent().getExtras();
         // dataBundle contains the notification data payload
    }
}

【讨论】:

  • 谢谢!!!!!!它对我有用,通知数据来自 SplashActivity 意图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多