我在 firebase 中使用通知编辑器通过自定义数据发送以下键值对:
type : 555
我没有在通知编辑器中将双引号 ""
放在 type
和 555
周围。所以在 key 的地方我写了 type
而在 value 的地方我写了 555
.
以下是我检索此类型值的代码:
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
final Map<String, String> data = remoteMessage.getData();
final String type = data.get("type");
Log.e("TAG","Type= "+type);
}
但是我得到的日志输出是:
Type= null
为什么我得到的是空值?如何正确检索给定键的值?
编辑 看来
final Map<String, String> data = remoteMessage.getData();
一切正常。我在这一行之后放置了一个 Log.e()
语句,它正确地打印了 555。所以看起来 data.get("type")
正在返回 null,即使键“类型”存在。为什么会这样?
最佳答案
我遇到了同样的问题。经过一番搜索,我想出了以下解决方案。
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
RemoteMessage.Notification data = remoteMessage.getNotification();
String title = data.getTitle();
String body = data.getBody();
if (data != null){
// do some work
}
}
我试过了,它对我有用。我希望它能帮助其他人。
关于android - 为什么 RemoteMessage.getData() 在 firebase 通知中返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44351415/