怎么算的Andr​​oid GCM负载长度 [英] how to count android gcm payload length

查看:128
本文介绍了怎么算的Andr​​oid GCM负载长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的GCM文件,据说有效载荷为4096字节的限制。

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

不过,我发现我可以发送有效载荷与16834轮空。

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);


此外,什么是从GCM响应如果有效载荷太长?


Furthermore, what's the response from GCM if the payload is too long?

推荐答案

如果负载过大,就会在错误消息得到MessageTooBig。
必须不超过4096的有效负载的一部分是所有在有效载荷中的定制键和值。你不要指望的注册ID,你不要指望predefined键,如生存时间和崩溃的关键。
顺便说一句,我发现,即使文件说,载荷不得超过4096字节,他们接受更大的有效载荷,只要不超过4096个字符(即你可以发送包含的字符连接$ c进行4096个字符的字符串$ cd到一个以上的字节,所以在字节的有效负载的长度会超过4096)。

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).

这篇关于怎么算的Andr​​oid GCM负载长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆