Size of notification payload in GCM/FCM

会有一股神秘感。 提交于 2020-05-09 21:44:20

问题


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.

I would like to know how to calculate the size of a GCM payload when it contains a "notification" dictionary.

I have been trying the Google Cloud Messaging service for Android. 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".

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.

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


回答1:


I experimented with payload sizes for the newer FCM service.

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

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. I couldn't figure out how the remaining 34 characters are counted.

This means the documentation that limits "notification" payload to 2K is incorrect. You can send close to 4K.

Now, reading the up-to-date documentation of FCM, I found that the documentation of Message types says:

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.

On the other hand, the description of the "MessageTooBig" error says:

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.

The messages I tested were not messages to topics, so according to both quotes, they should not be limited to 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).




回答2:


For downstream messaging, GCM provides two types of payload: notification and data. Notification is the more lightweight option, with a 2KB limit and a predefined set of user-visible keys. Data payload lets developers send up to 4KB of custom key/value pairs. 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. 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.




回答3:


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.

Store your payload inside a database and make it available via an Web-API. Now send a GCM-Message which holds just the ID of that database-entry. Now your app can request the payload independently of GCM and you are not limited to the size.

There is another advantage: Google will not know what you're sending over 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.




回答4:


FCM adds the prefix 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

For calculating notification payload, every key must be prefixed with gcm.notification.

for every key of notification payload, firebase internally adds 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.




回答5:


I also expermented with the payload size its near about 4 kb only.

When i tried to send a payload of size 7kb iy showed me a erroe in response that says message too big..

So you could parse the response code and verify that the payload was accepted by google servers or not.




回答6:


You can used the conventional method of converting the String data into bytes. The notification you get in form of JSON that contains key/value pair as:

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

As per Google Documentation:

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.

To convert the bytes into kB, just divide by 1024. For more details you can refer this.



来源:https://stackoverflow.com/questions/31264206/size-of-notification-payload-in-gcm-fcm

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