这个问题最初是指 Google Cloud Messaging (GCM),但现在也适用于取代 GCM 的新 Firebase Cloud Messaging (FCM)。
我想知道如何计算包含“通知”字典的 GCM 负载的大小。
我一直在试用适用于 Android 的 Google 云消息服务。文档的某些部分说您最多可以发送 4KB 的数据,并且 here它说“通知消息最多可以有 2kb 的有效负载”。
做一些测试后,我可以发送带有“数据”有效负载且填充了 4KB 数据的消息,并且服务器按预期无误地接受了它们。
但是,使用“通知”负载我发现我可以发送超过 2KB 数据的消息并且服务器没有返回错误。我预计这样的消息会太大。
我发现“通知”负载与“数据”负载共享允许的 4KB,但方式不同。在“数据”负载中,您可以通过添加键和值的大小来计算大小。 “通知”负载占用的空间比它包含的键和值的大小要多。
当负载包含“通知”字典时,如何预先计算负载的大小?
最佳答案
我试验了较新的 FCM 服务的负载大小。
对于包含“数据”字典但没有“通知”字典的消息,我成功地发送了最多 4096 个字符(计算所有键和值的长度)。
对于包含“通知”字典但没有“数据”字典的消息,以及同时包含“通知”字典和“数据”字典的消息,我设法发送了最多 4062 个字符。我想不通剩下的 34 个字符是如何计算的。
这意味着将“通知”负载限制为 2K 的文档是不正确的。您可以发送接近 4K 的图像。
现在,阅读 FCM 的最新文档,我发现 the documentation of Message types说:
Notification messages contain a predefined set of user-visible keys. Data messages, by contrast, contain only your user-defined custom key-value pairs. Notification messages can contain an optional data payload. Maximum payload for both message types is 4KB, except when sending messages from the Firebase console, which enforces a 1024 character limit.
另一方面,the description of the "MessageTooBig" error说:
Check that the total size of the payload data included in a message does not exceed FCM limits: 4096 bytes for most messages, or 2048 bytes in the case of messages to topics. This includes both the keys and the values.
我测试的消息不是主题消息,所以根据这两个引用,它们应该不限于2K。
因此,根据当前文档,负载限制为 4K(主题消息可能异常(exception),我没有测试过)。
关于android - GCM/FCM 中通知负载的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31264206/