drmgg4411 2017-02-16 22:23
浏览 79
已采纳

在json中转换fcm通知响应

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
}]

I am getting above response from fcm and I want to convert it into json please check above android code.

getting response from remoteMessage.
I am not getting data in getBody().
getting null value only.

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");
 }

展开全部

  • 写回答

1条回答 默认 最新

  • douyong1885 2017-02-16 22:28
    关注

    From your response snippet, I believe your talking about how should you do it on Android.

    It is in JSON but wrapped in your data bundle, that you have received as argument in FCM receiver function try getting values from your bundle, as usually we do in android.

    Rather than thinking "JSON", think "key/value pairs". Your server sends data in key/value pairs. Your app receives that data as key/value pairs in the extras in the Intent you get. You know what your keys are, so just retrieve the values out of the extras that are tied to those keys (e.g., getStringExtra("message"), getStringExtra("title"))

    Hope this solves your prob.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 IEd中开关量采样信号通道设计
  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 求GCMS辅导数据分析
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部