从 FCM onMessageReceived 方法的 RemoteMessage 获取值

新手上路,请多包涵

我已经迁移 gcm to fcm 用于推送通知消息。但是我如何从 RemoteMessage 获取捆绑数据收到 onMesssageReceived 方法。

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

所以请告诉我如何解析 remotemessage 以获得通知的所有价值。

我的工资单

{
"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"
]
}

原文由 Jatinkumar Patel 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 653
2 个回答

在 FCM 中,您收到的是 RemoteMessage 而不是 Bundle。

下面是我在我的应用程序中使用的方式,其中数据是我的 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();

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

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

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

原文由 Drup Desai 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是几乎不言自明的代码片段。

您以地图的形式获取数据

public void onMessageReceived(RemoteMessage remoteMessage)
        {
            Log.e("dataChat",remoteMessage.getData().toString());
            try
            {
                Map<String, String> params = remoteMessage.getData();
                JSONObject object = new JSONObject(params);
                Log.e("JSON_OBJECT", object.toString());
          }
       }

确保从服务器以正确的格式发送数据,即在“数据”键中

这是演示 Json 文件

{
  "to": "registration_ids",
  "data": {
    "key": "value",
    "key": "value",
    "key": "value",
    "key": "value"
  }
}

原文由 Pritish Joshi 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题