Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 671
jorcasso Avatar asked Jul 07 '15 09:07

jorcasso


People also ask

What is payload in FCM?

Firebase Cloud Messaging (FCM) is a messaging solution that lets you reliably send messages at no cost to both Android & iOS devices. Using FCM, you can send data payloads (via a message) to a device for a specific application. Each message can transfer a payload of up to 4KB to a client.

Does FCM have limit?

You can send up to 240 messages/minute and 5,000 messages/hour to a single device.

What is payload in notification?

Each notification your provider server sends to the Apple Push Notification service (APNs) includes a payload. The payload contains any custom data that you want to send to your app and includes information about how the system should notify the user.


1 Answers

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

like image 64
Eran Avatar answered Sep 28 '22 06:09

Eran