首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在json中转换fcm通知响应

在json中转换fcm通知响应
EN

Stack Overflow用户
提问于 2017-02-17 06:23:55
回答 1查看 388关注 0票数 0
代码语言:javascript
运行
复制
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()中获取数据。

只获取空值。

代码语言:javascript
运行
复制
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");
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-17 06:28:18

从你的回应片段来看,我相信你会说你应该如何在Android上做这件事。

您在FCM接收函数中作为参数接收到的是JSON,但包装在数据包中,尝试从包中获取值,就像我们在android中通常所做的那样。 与其考虑"JSON",不如考虑“键/值对”。您的服务器以键/值对的形式发送数据。您的应用程序接收这些数据作为额外的键/值对,这是你得到的意图。您知道您的键是什么,所以只需从绑定到这些键的附加值(例如,getStringExtra("message")、getStringExtra("title"))中检索值即可。

希望这能解决你的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42290795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档