简体   繁体   English

如何计算android gcm的有效负载长度

[英]how to count android gcm payload length

In the android GCM document, it is said the payload has a limit of 4096 bytes. 在android GCM文档中,据说有效负载的限制为4096字节。

However, I found that I can send a payload with 16834 byes. 但是,我发现我可以发送16834个有效载荷。

Did I make a mistake? 我犯了错误吗? I computed the length as follows: 我按如下方式计算了长度:


Map<Object, Object> jsonRequest = new HashMap<Object, Object>();
setJsonField(jsonRequest, GCMConstants.PARAM_TIME_TO_LIVE, message.getTimeToLive());
setJsonField(jsonRequest, GCMConstants.PARAM_COLLAPSE_KEY, message.getCollapseKey()); 
setJsonField(jsonRequest, GCMConstants.PARAM_DELAY_WHILE_IDLE, message.isDelayWhileIdle());
jsonRequest.put(GCMConstants.JSON_REGISTRATION_IDS, registrationIds); 
Map<String, Object> payload = message.getData(); 
if (!payload.isEmpty()) { 
    jsonRequest.put(GCMConstants.JSON_PAYLOAD, payload);
}  
String requestBody = gson.toJson(jsonRequest); 
System.out.println(requestBody.getBytes("UTF-8").length);

Furthermore, what's the response from GCM if the payload is too long? 此外,如果有效载荷太长,GCM的响应是什么?

If the payload is too big, you'll get "MessageTooBig" in the error message. 如果有效负载太大,您将在错误消息中获得“MessageTooBig”。 The part of the payload that must not exceed 4096 is all the custom keys and values in the payload. 有效负载中不得超过4096的部分是有效负载中的所有自定义密钥和值。 You don't count the registration ids and you don't count predefined keys such as time to live and collapse key. 您不计算注册ID,也不计算预定义的密钥,例如生存时间和折叠密钥。 By the way, I found out that even though the documentation says the payload must not exceed 4096 bytes, they accept larger payloads as long as they don't exceed 4096 characters (ie you can send a 4096 characters string that contains characters encoded into more than one byte, so the length of the payload in bytes will exceed 4096). 顺便说一句,我发现尽管文档说有效负载不得超过4096字节,但只要它们不超过4096个字符,它们就会接受更大的有效负载(即你可以发送一个4096个字符的字符串,其中包含编码为更多的字符超过一个字节,因此有效载荷的长度(以字节为单位)将超过4096)。

But as of today it allowed till 4089 characters to be sent via GCM and 511 characters were visible in the notification panel when its slide down. 但截至今天,它允许通过GCM发送直到4089个字符,并且当它向下滑动时,通知面板中可以看到511个字符。 Have practically tested it 实际测试过它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM