如何从 firebase 云消息传递中获取对象通知的值

How to get values of object notification from firebase cloud messaging

服务器像这样在通知中向我发送数据:

{    "to" : "xxxxxxxxxx",  
     "notification": 
       {
         "badge": "1",
         "bodyLocArgs": "['XXXX']",
         "bodyLocKey": "AAA.BBB.CCC",
         "image": "url_image",
         "sound": "default"
       },
    "data": {
        "friend": "5dde6ffe47f65624004556d1",
        "type": "FRA"
    } }

在我的函数 onMessageReceived 中我无法获取对象 "notification" 的值,我尝试了 remoteMessage.getNotification().getBody() 但它总是给我 null , 在 remoteMessage.getData() 中我只找到对象 "data" 与 "friend" 和 "type", 所以我的问题是如何获取对象 "notification"

通知没有 body 或标题。所以 .getBody() 总是空的。

你可以这样使用:

String bodyLocKey = remoteMessage.getNotification().getBodyLocalizationKey();
String [] bodyLocArgs = remoteMessage.getNotification().getBodyLocalizationArgs();

 remoteMessage.getNotification().getBody(); // null
 remoteMessage.getNotification().getTitle(); // null

更多methods

发送这种格式的通知对我来说非常有用,在后台、前台和被杀死的应用程序中,问题出在 child "notification"

{    "to" : "xxxxxxxxxx",  

    "data": {
         "badge": "1",
         "bodyLocArgs": "['XXXX']",
         "bodyLocKey": "AAA.BBB.CCC",
         "image": "url_image",
         "sound": "default"
         "friend": "5dde6ffe47f65624004556d1",
         "type": "FRA"
    } }

remotemessage.getData() 我找到了我所有的通知值。