微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从系统托盘android firebase FCM中的通知中检索通知值

我正在使用firebase向我的应用发送通知.我想将每台设备上收到的通知保存在本地数据库中.问题是,当应用程序在后台并收到通知时,将不会调用onMessageReceived()方法,而是根据文档将通知添加到系统托盘中.该文档还提到以下用于在应用程序处于后台时收到通知

In these cases,the notification is delivered to the device’s system tray,and the data payload is delivered in the extras of the intent of your launcher Activity.

因此,当我尝试访问intent中的extras时,找不到通知的键.
我在MainActivity.java中的代码

if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Toast.makeText(this,"Key: " + key + " Value: " + value,Toast.LENGTH_LONG).show();
        }
    }

我们从服务器发送的通知

{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...","notification" : {
  "body" : "great match!","title" : "Portugal vs. Denmark","icon" : "myicon"
  }
}

任何人都知道如何检索通知中的值?

解决方法

这是预期的行为(正如您在 documentation中看到的那样).如果您正在使用仅通知消息有效负载(您是),则只有在您的应用处于前台时才会调用onMessageReceived(),否则系统托盘将接收它.

在(强调我的)中提到的数据:

In these cases,and the data payload is delivered in the extras of the intent of your launcher Activity.

是数据消息有效负载(见Message Types).

您必须发送包含通知和数据的有效负载.像这样:

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...","notification" : {
    "body" : "great match!","icon" : "myicon"
    },"data": {
    "body" : "great match!","icon" : "myicon"
  }
}

另见my answer here.

原文地址:https://www.jb51.cc/android/315702.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐