Bundle
[{
google.sent_time=1487229288769,
gcm.notification.created_at=2017-02-16 12:44:52,
gcm.notification.e=1,
gcm.notification.Content_available=1,
gcm.notification.badge=1,
gcm.notification.title=, from=388143837768,
google.message_id=0:1487229288775757%19aca14d19aca14d,
gcm.notification.body=hi,
gcm.notification.uid=160,
collapse_key=com.sid.Smessenger
}]我得到了来自fcm的以上响应,我想把它转换成json,请检查上面的android代码。
从remoteMessage那里得到回应。
我不会在getBody()中获取数据。
只获取空值。
public void onMessageReceived(RemoteMessage remoteMessage)
{
Log.d(TAG, "FROM: " + remoteMessage.getFrom());
remoteMessage.getFrom();
if (remoteMessage.getData().size() > 0)
{
Log.d(TAG, "Message data: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null)
{
Log.d(TAG, "Message body:" +
remoteMessage.getNotification().getBody());
String questionId =
remoteMessage.getData().get("gcm.notification.gid");
String userId =
remoteMessage.getData().get("gcm.notification.uid");
Log.e("Group ID", questionId);
Log.e("U ID", userId);
Bundle bundle = new Bundle();
for (Map.Entry<String, String> entry :
remoteMessage.getData().entrySet()) {
bundle.putString(entry.getKey(), entry.getValue());
Log.e(entry.getKey(), entry.getValue());
//run it/// and get notification///
}
// String created_at = remoteMessage.
sendNotification(remoteMessage.getNotification().getBody());
}
// String value = bundle.getString("request");
}发布于 2017-02-17 06:28:18
从你的回应片段来看,我相信你会说你应该如何在Android上做这件事。
您在FCM接收函数中作为参数接收到的是JSON,但包装在数据包中,尝试从包中获取值,就像我们在android中通常所做的那样。 与其考虑"JSON",不如考虑“键/值对”。您的服务器以键/值对的形式发送数据。您的应用程序接收这些数据作为额外的键/值对,这是你得到的意图。您知道您的键是什么,所以只需从绑定到这些键的附加值(例如,getStringExtra("message")、getStringExtra("title"))中检索值即可。
希望这能解决你的问题。
https://stackoverflow.com/questions/42290795
复制相似问题