简体   繁体   English

GCM / FCM中通知有效载荷的大小

[英]Size of notification payload in GCM/FCM

This question was originally referring to Google Cloud Messaging (GCM), but now it also applies to the new Firebase Cloud Messaging (FCM) that replaces GCM. 这个问题原本是指Google Cloud Messaging(GCM),但现在也适用于取代GCM的新Firebase Cloud Messaging(FCM)。

I would like to know how to calculate the size of a GCM payload when it contains a "notification" dictionary. 我想知道当GCM有效载荷包含“通知”字典时如何计算其大小。

I have been trying the Google Cloud Messaging service for Android. 我一直在尝试Android的Google Cloud Messaging服务。 Some parts of the documentation say you can send up to 4KB of data, and here it says "A notification message can have a maximum of 2kb payload". 文档的某些部分说,您最多可以发送4KB的数据,并在此处显示“通知消息最多可以包含2kb的有效负载”。

Doing some tests I could send messages with the "data" payload filled with 4KB of data and the server accepted them without error as expected. 做一些测试,我可以发送带有4KB数据的“数据”有效负载的消息,并且服务器接受了它们,而没有出现预期的错误。

However, using the "notification" payload I found that I could send messages with more than 2KB of data and the server did not return error. 但是,使用“通知”有效负载,我发现我可以发送包含2KB以上数据的消息,并且服务器未返回错误。 I expected such messages would be too big. 我希望这样的消息太大。

I found that the "notification" payload shares the allowed 4KB with the "data" payload, but not in the same way. 我发现“通知”有效负载与“数据”有效负载共享了允许的4KB,但不是以相同的方式。 In the "data" payload, you can calculate the size by adding the size of the keys and values. 在“数据”有效负载中,可以通过添加键和值的大小来计算大小。 The "notification" payload takes up more space than the size of the keys and values it contains. “通知”有效负载所占空间比其所包含的键和值的大小大。

How can I calculate in advance the size of a payload when it contains the "notification" dictionary? 当有效负载包含“通知”字典时,如何提前计算其大小?

I experimented with payload sizes for the newer FCM service. 我为更新的FCM服务尝试了有效负载大小。

For messages that contain a "data" dictionary and no "notification" dictionary, I managed to send exactly up to 4096 characters (counting the lengths of all the keys and values). 对于包含“数据”字典而没有“通知”字典的消息,我设法发送多达4096个字符(计算所有键和值的长度)。

For messages that contain a "notification" dictionary and no "data" dictionary, as well as for messages that contain both a "notification" dictionary and a "data" dictionary I managed to send up to 4062 characters. 对于包含“通知”字典而没有“数据”字典的消息,以及同时包含“通知”字典和“数据”字典的消息,我设法发送了多达4062个字符。 I couldn't figure out how the remaining 34 characters are counted. 我不知道如何计算其余的34个字符。

This means the documentation that limits "notification" payload to 2K is incorrect. 这意味着将“通知”有效载荷限制为2K的文档不正确。 You can send close to 4K. 您可以发送接近4K的数据。

Now, reading the up-to-date documentation of FCM, I found that the documentation of Message types says: 现在,阅读FCM的最新文档后,我发现Message类型的文档说:

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. 两种消息类型的最大有效负载为4KB ,从Firebase控制台发送消息时(强制实施1024个字符限制)除外。

On the other hand, the description of the "MessageTooBig" error says: 另一方面, “ MessageTooBig”错误的描述为

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 . 检查消息中包含的有效载荷数据的总大小不超过FCM限制:大多数消息为4096字节, 对于主题消息则为2048字节 This includes both the keys and the values. 这包括键和值。

The messages I tested were not messages to topics, so according to both quotes, they should not be limited to 2K. 我测试的消息不是针对主题的消息,因此根据两个引号,它们不应限于2K。

Therefore the payload limit according to the current documentation is 4K (with the possible exception of messages to topics, which I didn't test). 因此,根据当前文档,有效负载限制为4K(可能未发送主题消息,但我没有测试)。

For downstream messaging, GCM provides two types of payload: notification and data. 对于下游消息传递,GCM提供了两种有效载荷:通知和数据。 Notification is the more lightweight option, with a 2KB limit and a predefined set of user-visible keys. 通知是一种更轻量级的选项,具有2KB的限制和一组预定义的用户可见键。 Data payload lets developers send up to 4KB of custom key/value pairs. 数据有效载荷使开发人员最多可以发送4KB的自定义键/值对。 Notification messages can contain an optional data payload which is delivered when users click on the notification. 通知消息可以包含可选的数据有效负载,当用户单击通知时会传递该数据有效负载。

Notification - GCM automatically displays the message to end user devices on behalf of the client app. 通知 -GCM代表客户端应用自动将消息显示给最终用户设备。 Notifications have a pre-defined set of user-visible keys. 通知具有一组预定义的用户可见键。 Set notification payload. 设置通知有效负载。 May have optional data payload. 可能具有可选的数据有效负载。 Always collapsible. 始终可折叠。

Data - Client app is responsible for processing data messages. 数据 -客户端应用负责处理数据消息。 Data messages have only custom key/value pairs. 数据消息仅具有自定义键/值对。 Set data payload only. 仅设置数据有效负载。 Can be either collapsible or non-collapsible. 可以是可折叠的或不可折叠的。

This might not be for what you explicit asked for but better don't use that large amount of payload-data within your GCM-Message. 这可能不是您明确要求的,但最好不要在GCM-Message中使用大量的有效载荷数据。

Store your payload inside a database and make it available via an Web-API. 将您的有效负载存储在数据库中,并通过Web-API使其可用。 Now send a GCM-Message which holds just the ID of that database-entry. 现在发送一条GCM消息,其中仅包含该数据库条目的ID。 Now your app can request the payload independently of GCM and you are not limited to the size. 现在,您的应用程序可以独立于GCM来请求有效负载,并且您不受大小限制。

There is another advantage: Google will not know what you're sending over GCM. 还有一个优势:Google不会知道您通过GCM发送的内容。

If you don't need to store the payload for long you could also use Redis or something similar to store that payload for a limited time. 如果您不需要长时间存储有效负载,则还可以使用Redis或类似的方法在有限时间内存储该有效负载。

FCM adds the prefix gcm.notification. FCM添加前缀gcm.notification。 for every key in notification payload. 通知有效负载中的每个键。

Sample calculation for the below payload: 以下有效负载的示例计算:

  "to":"cgOtBDOGIEc:APA91bGrjdPtrnGr0sIl4c66Z3Xp-JTzUasIN5TzWy7DtNUf-BlGvF64iNOXFN68zFC6oTYHJbP6eQgzIZICcsmIUG-NP5cIXf8EyPNiIAvOFU27XDKFbI2vowMjsNmZQdmh",


  "notification":{
    "title":"Testing title from postman!",
    "body":"Testing body from postman!",
    "sound":"default",
    "tickerText":"This is ticker text"
  },
  "data" : {
     "Nick" : "Mario Test",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   }
}

for above payload, 对于上述有效载荷,

Total length = length of (Notification payload + Data payload) 总长度=的长度(通知有效负载+数据有效负载)

Length of Data Payload = [ length of keys + length of values ] = length of bytes of [ Nick + body + Room ] + length of bytes of [Mario Test + great match + PortugalVSDenmark ] = 12 + 39 = 51 数据有效负载的长度= [键的长度+值的长度] = [尼克+身体+房间]的字节长度+ [马里奥测试+完美匹配+ PortugalVSDenmark]的字节长度= 12 + 39 = 51

For calculating notification payload, every key must be prefixed with gcm.notification. 为了计算通知有效负载,每个键必须带有gcm.notification前缀。

for every key of notification payload, firebase internally adds gcm.notification. 对于通知有效负载的每个键,firebase在内部添加gcm.notification。 as the prefix, and calculates the length considering this prefix also. 作为前缀,并考虑该前缀来计算长度。

Length of Notification Payload  =  [ no.of keys * length of (gcm.notification.)  +  length of keys + length of values ] 

                                 = 4*17 + length of bytes of [ title + body + sound + tickerText ] + length of bytes of [ Testing title from postman! + Testing body from postman! + default + This is ticker text ]

                                                   = 68 + 24 +79

                                                   =  171 bytes

Total length of the payload = 51 + 171 =  222 bytes.                        

Hope this answers your question. 希望这能回答您的问题。

I also expermented with the payload size its near about 4 kb only. 我还对有效负载大小进行了实验,其有效负载大小仅约4 kb。

When i tried to send a payload of size 7kb iy showed me a erroe in response that says message too big.. 当我尝试发送大小为7kb的有效载荷时,我向我显示了一条错误消息,说消息太大。

So you could parse the response code and verify that the payload was accepted by google servers or not. 因此,您可以解析响应代码并验证Google服务器是否接受了有效负载。

You can used the conventional method of converting the String data into bytes. 您可以使用将String数据转换为字节的常规方法。 The notification you get in form of JSON that contains key/value pair as: 您以JSON形式收到的通知,其中包含键/值对,如下所示:

{
  "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark"
  }
}

As per Google Documentation: 根据Google文档:

Now calculate the data size using: 现在使用以下方法计算数据大小:

String mydata = "value from JSON";
byte[] mybyte = mydata.getBytes("UTF-8");
int bytes = mybyte.length;

Make sure that you specify the encoding of the String, as different encodings may take up a different number of bytes for the same String. 确保指定字符串的编码,因为对于同一字符串,不同的编码可能占用不同数量的字节。 In the above example, UTF-8 is used as the encoding. 在上面的示例中,UTF-8被用作编码。

To convert the bytes into kB, just divide by 1024. For more details you can refer this . 要将字节转换为kB,只需除以1024。有关更多详细信息,请参考this

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

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