GCM 消息(或数据)限制


GCM message(Or data ) limit

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

感谢阿达文斯

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

经过几次尝试,我发现消息的数据部分的限制不能大于 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!");