从 FCM onMessageReceived 方法的 RemoteMessage 获取值
Get value from RemoteMessage from FCM onMessageReceived method
我已迁移 gcm to fcm
推送通知消息。
但是我如何从 RemoteMessage 获取捆绑数据收到 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"
]
}
这里是代码片段,几乎可以自我解释。
你得到的是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());
}
}
确保从服务器发送的数据格式正确,即 "data" 键
这里是演示Json文件
{
"to": "registration_ids",
"data": {
"key": "value",
"key": "value",
"key": "value",
"key": "value"
}
}
在 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 中收到映射并将这些字段转换为适当的数据类型,因为所有这些数据都以字符串形式出现。
您的数据如下:
"data":{
"message": "Message for new task",
"time": "6/27/2016 5:24:28 PM"
}
你应该让他们通过
Log.d(TAG, "Key Data : " + remoteMessage.getData().get("message").toString());
Log.d(TAG, "Key Data : " + remoteMessage.getData().get("time").toString());
将它们包裹在 try catch 中以确保
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
sendNotification(notification, data);
String urlparam=data.get("url_param").toString();
String urlparam=data.get("dp_name").toString();
"data": {
"notification_id": 11,
"title_url": "service_id=1;service_category=Installation;",
"destination": "Category_page"
}
getData()
的乐趣类型是 Map<String, String>
。所以你可以像我们通常使用 Map<String, String>
.
那样获取 data
对象值
"data": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4"
}
然后你必须在 kotlin 中像这样获取
val data = remoteMessage.data
val key1 = data[key1]
val key2 = data[key2]// you can fetch like this
对于getNotification()
类型是Notification
对象
val notification: Notification = remoteMessage.notification
val message = notification.body
val title = notification.title // you can fetch by accessing Notification class member
这里是关于 Notification 对象的详细信息
我已迁移 gcm to fcm
推送通知消息。
但是我如何从 RemoteMessage 获取捆绑数据收到 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"
]
}
这里是代码片段,几乎可以自我解释。
你得到的是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());
}
}
确保从服务器发送的数据格式正确,即 "data" 键
这里是演示Json文件
{
"to": "registration_ids",
"data": {
"key": "value",
"key": "value",
"key": "value",
"key": "value"
}
}
在 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 中收到映射并将这些字段转换为适当的数据类型,因为所有这些数据都以字符串形式出现。
您的数据如下:
"data":{
"message": "Message for new task",
"time": "6/27/2016 5:24:28 PM"
}
你应该让他们通过
Log.d(TAG, "Key Data : " + remoteMessage.getData().get("message").toString());
Log.d(TAG, "Key Data : " + remoteMessage.getData().get("time").toString());
将它们包裹在 try catch 中以确保
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
sendNotification(notification, data);
String urlparam=data.get("url_param").toString();
String urlparam=data.get("dp_name").toString();
"data": {
"notification_id": 11,
"title_url": "service_id=1;service_category=Installation;",
"destination": "Category_page"
}
getData()
的乐趣类型是 Map<String, String>
。所以你可以像我们通常使用 Map<String, String>
.
data
对象值
"data": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4"
}
然后你必须在 kotlin 中像这样获取
val data = remoteMessage.data
val key1 = data[key1]
val key2 = data[key2]// you can fetch like this
对于getNotification()
类型是Notification
对象
val notification: Notification = remoteMessage.notification
val message = notification.body
val title = notification.title // you can fetch by accessing Notification class member
这里是关于 Notification 对象的详细信息