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 格式
基础教程推荐
- Android Volley - 如何动画图像加载? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
