通过 FCM onMessageReceived 方法从 RemoteMessage 获取值

我已将 gcm 迁移到 fcm 以获取推送通知消息.但是我如何从收到的 RemoteMessage 中获取捆绑数据的 onMesssageReceived 方法.

I have migrate gcm to fcm for push notification message. but how I Get bundle data from RemoteMessage received onMesssageReceived method.

Old GCM give bundle data onMessageReceiced method but in FCM there is RemoteMessage data.

所以请告诉我如何解析远程消息以获取通知的所有值.

So please tell me how I parse remotemessage for get all value of notification.

我的工资单

{
"collapse_key":"score_update",
"priority":"high",
"content_available":true,
"time_to_live":108,
"delay_while_idle":true,
"data": 
{ 
    "message": "Message for new task",
    "time": "6/27/2016 5:24:28 PM"
},
"notification": {
    "sound": "simpleSound.wav",
    "badge": "6",
    "title": "Test app",
    "icon": "myicon",
    "body": "hello 6 app",
    "notification_id" : "1140",
    "notification_type" : 1,
    "notification_message" : "TEST MESSAGE",
    "notification_title" : "APP"
  },
"registration_ids": ["cRz9SJ-gGuo:APA91bFJPX7_d07AR7zY6m9khQro81GmSX-7iXPUaHqqcOT0xNTVsOZ4M1aPtoVloLNq71-aWrMCpIDmX4NhMeDIc08txi6Vc1mht56MItuVDdA4VWrnN2iDwCE8k69-V8eUVeK5ISer"
]
}

推荐答案

在 FCM 中你收到的是 RemoteMessage 而不是 Bundle.

In FCM you received RemoteMessage instead of Bundle.

以下是我在应用程序中使用的方式,其中数据是我的 RemoteMessage

Below is the way I used in my application where data is my RemoteMessage

Map<String, String> data = remoteMessage.getData()
int questionId = Integer.parseInt(data.get("questionId").toString());
String questionTitle = data.get("questionTitle").toString();
String userDisplayName = data.get("userDisplayName").toString();
String commentText = data.get("latestComment").toString();

以下是我从服务器发送的通知数据

Below is my notification data which I am sending it from server

{
  "registration_ids": "",
  "data": {
    "questionId": 1,
    "userDisplayName": "Test",
    "questionTitle": "Test",
    "latestComment": "Test"
  }
}

因此,您必须根据您的回复解析每个字段.由于我已经调试了代码,您将在 RemoteMessage 中收到地图,并将这些字段转换为适当的数据类型,因为所有这些数据都以字符串形式出现.

So you have to parse each and every field as per your response. As I have debugged the code you will receive map in your RemoteMessage and cast those fields in appropriate data types as all those data comes as string.

相关文章