【问题标题】:GCM message(Or data ) limitGCM 消息(或数据)限制
【发布时间】:2015-03-12 14:47:51
【问题描述】:

我有用于发送推送通知的 php 代码。它适用于小数据(消息),但是当我尝试在推送通知中发送大字符串时它不起作用。在 GCM 中发送的字符串是否有任何限制?如果有的话,尺寸是多少?

谢谢您

【问题讨论】:

    标签: php google-cloud-messaging


    【解决方案1】:

    是的,消息的负载(包括负载中的所有自定义键和值)有 4096 字节的限制。

    【讨论】:

      【解决方案2】:

      经过几次尝试,我发现消息的数据部分的限制不能大于2048字节。 问题是,我对数据使用 UTF-8 编码,我认为这可能是原因。 数据是一个小文本文件的内容。

      // Prepare JSON containing the GCM message content.
      JSONObject jGcmData = new JSONObject();
      jGcmData.put("to", "/topics/"+args[0].trim());
      // Set maximum time alive in seconds.
      jGcmData.put("time_to_live", 300);
      // Set the collapse key, which groups as one the messages of same topic
      jGcmData.put("collapse_key", args[0].trim());
      // Prepare the GCM data
      JSONObject jData = new JSONObject();
      String message = readFile(args[1].trim(), Charset.forName("UTF-8"));
      jData.put("message", message);
      System.out.println("Data length: " + message.length());
      // Add the data to the message.
      jGcmData.put("data", jData);
      System.out.println("Total GCM length: " + jGcmData.toString().getBytes("UTF-8").length);
      // Create connection to send GCM Message request.
      URL url = new URL("https://android.googleapis.com/gcm/send");
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestProperty("Authorization", "key=" + API_KEY);
      conn.setRequestProperty("Content-Type", "application/json");
      conn.setRequestMethod("POST");
      conn.setDoOutput(true);
       // Send GCM message content.
      DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
      outputStream.write(jGcmData.toString().getBytes("UTF-8"));
      outputStream.flush();
      outputStream.close();
      // Read GCM response.
      InputStream inputStream = conn.getInputStream();
      String resp = IOUtils.toString(inputStream);
      System.out.println(resp);
      System.out.println("SUCCESS!");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多