java - 通过 FCM onMessageReceived 方法从 RemoteMessage 获取值

标签 java android google-cloud-messaging android-notifications firebase-cloud-messaging

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

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

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

我的工资单

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

最佳答案

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

你以Map的形式获取数据

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

关于java - 通过 FCM onMessageReceived 方法从 RemoteMessage 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38244332/

相关文章:

java - 如何在 JCR SQL2 查询中转义动态生成的字符串值?

java - 在android java中加载资源的最佳方法

java - 如何检索 "standard"Android 联系人

javascript - 是否可以使用 Firebase Cloud Messaging API 列出所有推送通知?

android - 通过Gradle测试Google Cloud Messaging Push通知

java - 无法从 START_ARRAY token 中反序列化对象的实例

java - 虚拟设备中没有后退按钮

android - 谷歌云消息不发送通知

java - openshift eclipse 发布卡住

android:showAsAction ="always"不适用于选项卡 fragment