how to count android gcm payload length

我只是一个虾纸丫 提交于 2019-12-10 13:07:42

问题


In the android GCM document, it is said the payload has a limit of 4096 bytes.

However, I found that I can send a payload with 16834 byes.

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?


回答1:


If the payload is too big, you'll get "MessageTooBig" in the error message. The part of the payload that must not exceed 4096 is all the custom keys and values in the payload. You don't count the registration ids and you don't count predefined keys such as time to live and collapse key. 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 (i.e. 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).




回答2:


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. Have practically tested it



来源:https://stackoverflow.com/questions/13698621/how-to-count-android-gcm-payload-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!