【问题标题】:JSON gcm message from server not being recognised on the android clientandroid客户端无法识别来自服务器的JSON gcm消息
【发布时间】:2013-02-25 07:22:36
【问题描述】:

我正在尝试发送和接收 JSON GCM 消息,但在客户端上它似乎总是以 UTF8 格式发送。

protected HttpURLConnection post(String url, String contentType, String body)
throws IOException {
    Print.logInfo("In HttpURLConnection: " + contentType + body);
    byte[] bytes = body.getBytes();
    HttpURLConnection conn = getConnection(url);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setFixedLengthStreamingMode(bytes.length);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", contentType);
    conn.setRequestProperty("Authorization", "key=" + key);
    OutputStream out = conn.getOutputStream();
    out.write(bytes);
    out.close();
    return conn;
}

在服务器上,UTF8 消息是: 在 HttpURLConnection 中: application/x-www-form-urlencoded;charset=UTF-8registration_id=APA91...&delay_while_idle=0&collapse_key=Test&time_to_live=2419200&data.Data1=Value+1&data.Data2=Value+2&data.Data3=Value+3

JSON 消息是: 在 HttpURLConnection: application/json{"delay_while_idle":false,"collapse_key":"Test","data":{"Data1":"Value 1","Data2":"Value 2","Data3":"Value 3"},"time_to_live":2419200,"registration_ids":["APA91..."]}

带有多播结果 MulticastResult(multicast_id=5036...,total=1,success=1,failure=0,canonical_ids=0,results: [[ messageId=0:1362.. ]]:

两个消息都在客户端成功接收,但我无法识别 json 格式。

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String inAction = intent.getAction();
    // check to see if it is a message
    if (inAction.equals("com.google.android.c2dm.intent.RECEIVE")) {
        // if your key/value is a JSON string, extract and parse it using JSONObject
        String json_info = intent.getExtras().getString("data");
        if (json_info != null) {
            try {
                JSONObject jsonObj = new JSONObject(json_info);
                payload = jsonObj.get("Data1")  + "\n"
                        + jsonObj.get("Data2")+ "\n"
                        + jsonObj.get("Data3");
                }
            catch (JSONException e) {
                // do nothing
                return;
            }
        }
        else {
            payload = intent.getStringExtra("Data1")  + "\n"
                    + intent.getStringExtra("Data2")+ "\n"
                    + intent.getStringExtra("Data3");
            }
    }
}

在这两种情况下 json_info 都是空的。我在运行 apache/tomcat 的服务器上使用 java servlet。

请帮忙。非常感谢

【问题讨论】:

  • 上次我遇到这样的问题是因为我在 json_encode 的字符串中有一些字符,在这种情况下,函数放入 null,我不了解 GCM,但也许这是情况也是如此。

标签: android android-intent push-notification google-cloud-messaging


【解决方案1】:

JSON 对象仅用于您的服务器与 GCM 服务器的通信。该应用程序永远不会获取 JSON 对象。通知的数据作为键/值对传递。如果出于某种原因想要传递 JSON 有效负载,则应将其编码为字符串并将该字符串作为 data JSON 对象内的键/值对之一的值传递。然后,您必须从意图中的额外内容中读取该值并解析 JSON。

onMessage(Context context, Intent intent):当你的服务器调用 向 GCM 发送消息,GCM 将消息传递给设备。如果 消息有一个有效载荷,它的内容可以作为额外的 意图。

其实这句话更贴切:

数据

一个 JSON 对象,其字段表示 消息的有效负载数据。如果存在,它将是有效载荷数据 作为应用程序数据包含在 Intent 中,键是 额外的名字。例如, "data":{"score":"3x1"} 会导致 意图额外命名的分数,其值为字符串 3x1。没有 键/值对的数量有限制,尽管 消息的总大小(4kb)。这些值可以是任何 JSON 对象, 但我们建议使用字符串,因为这些值将被转换为 无论如何,GCM 服务器中的字符串。如果您想包含对象或 其他非字符串数据类型(例如整数或布尔值),您有 自己转换为字符串。另请注意,密钥不能 是保留字(from 或任何以 google 开头的字)。到 稍微复杂一点,有一些保留字(如 collapse_key)在有效载荷数据中技术上允许的。然而, 如果请求中还包含该单词,则请求中的值将 覆盖有效载荷数据中的值。因此使用的词是 不建议在此表中定义为字段名称,即使在某些情况下 在技​​术上允许的地方。可选。

【讨论】:

  • Eran,非常感谢您抽出宝贵时间提供详细的回复。我最初对文档和堆栈溢出的一些示例感到困惑,这些示例表明 JSON 是直接传递给应用程序的。
猜你喜欢
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-13
相关资源
最近更新 更多