FCM Data payload received in android not in json format(在 android 中收到的 FCM 数据有效负载不是 json 格式)
问题描述
我从 Firebase 获取的数据负载不是 json 格式,而是获取自定义键值对,格式如下:
I am getting the data payload from the firebase not in json format, instead I am getting custom key-value pairs as following format:
Data Payload:{image=https://www.xxxx.xxx/get-profile-picture, message=This is a test message., senderName=Mathew John}
我必须使用 Json 解析来解析数据以进行进一步处理.这是我的代码:
I have to parse the data using Json parsing for further processing. Here is my code:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
String title = remoteMessage.getData().get("senderName");
System.out.println("raja" + title);
String msg = remoteMessage.getData().get("message");
System.out.println("raja" + msg);
sendMessage(msg,title);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
推荐答案
我从 firebase 获取的数据负载不是 json 格式
I am getting the data payload from the firebase not in json format
是的,它的行为符合预期.
Yes, Its behaving as intended.
因为数据负载包含自定义键值对而不是JSON格式
Because Data payload contains custom key-value pairs not a JSON format
我必须使用 Json 解析数据进行进一步处理.
I have to parse the data using
Jsonparsing for further processing.
您需要使用 Map<String, String> 将数据负载转换为 JSONObject
You need to use Map<String, String> to convert data payload in to a JSONObject
查看以下示例
示例代码
Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
Log.e("JSON_OBJECT", object.toString());
这篇关于在 android 中收到的 FCM 数据有效负载不是 json 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 android 中收到的 FCM 数据有效负载不是 json 格式
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
