I'm implementing C2DM on my android push application, this is what google said about collapse_key:
"An arbitrary string that is used to collapse a group of like messages when the device is offline, so that only the last message gets sent to the client. This is intended to avoid sending too many messages to the phone when it comes back online. Note that since there is no guarantee of the order in which messages get sent, the "last" message may not actually be the last message sent by the application server. Required."
I was wondering what if i want to get all the push sent during the device offline, what should i have in the collapse_key and how can i do that?
I think you should then make a new/different collaps-key for every message. You could just have an integer that keeps getting bigger over time or you could just use the system time...
While CD2M was deprecreated in favor for GCM, take into account that at least GCM allows to have only 4 collapse keys
GCM allows a maximum of 4 different collapse keys to be used by the
GCM server at any given time. In other words, the GCM server can
simultaneously store 4 different send-to-sync messages, each with a
different collapse key. If you exceed this number GCM will only keep 4
collapse keys, with no guarantees about which ones they will be.
Related
I have implemented Google Cloud Messaging for a user profile.
When the user updates the profile on the web app, it instantly sends a GCM request to GCM Server, and from there it is sent to my registered Android Device and the user profile is updated on the device.
But when the device is Offline, and the user updates the user profile two times. Two requests are sent when the device comes online, how to get only the recent request ?
Any help would be appreciated.
Thank you in advance.
That's what the collapse_key parameter is for. If you send two GCM messages with the same collapse_key to the same device, if the device is offline, the GCM server stores only the last message and will deliver a single message to the device when it gets back online.
Here's a quote about collapse_key :
If the device is connected but idle, the message will still be delivered right away unless the delay_while_idle flag is set to true. Otherwise, it will be stored in the GCM servers until the device is awake. And that's where the collapse_key flag plays a role: if there is already a message with the same collapse key (and registration ID) stored and waiting for delivery, the old message will be discarded and the new message will take its place (that is, the old message will be collapsed by the new one). However, if the collapse key is not set, both the new and old messages are stored for future delivery. Collapsible messages are also called send-to-sync messages.
(Source)
The syntax is simple. For example, if you are sending a JSON request, add the following :
"collapse_key" : "something",
The collapse key doesn't have to be unique for each device.
In the Google Cloud Messaging documentation about Messages with Payload I found the following information:
GCM will store up to 100 non-collapsible messages. After that, all messages are discarded from GCM, and a new message is created that tells the client how far behind it is.
But I can't find anything whether the 100 non-collapsible messages is the limit for the whole project or just per device?
best regards,
Phil
I think the limit if for per device, because the collapse key is also associate with registration ID.
Registration ID is tied to a particular Android application running on a particular device.
Also to note from the documentation:
However, if you use collapsible messages, remember that GCM only allows a maximum of 4 different collapse keys to be used by the GCM server per device at any given time. That potentially indicates that the messages are tie to per device.
StackOverflow, i need your help! I've read gcm documentation here http://developer.android.com/google/gcm/adv.html and don't understand fully what means "GCM only allows a maximum of 4 different collapse keys to be used by the GCM server per device at any given time". I want to explain for myself it. I can assume that if on my phone N applications are installed (facebook, twitter, instagramm, myMail, facebook and other), there is no guarantee that when my device for example will return to online, GCM will choose collapse_key of my app. Am i right or no?? If I am right in what sense of this collapse key if there is no guarantee that it will work and that my app will receive at the right time the necessary notification? I need to point that pattern "Sync-to-load" is very preferrable for me and i want to use it, but I am afraid that problems will begin when user will be write in comments "My device come back online, but i missed all messages:(("
The limit on GCM collapse_keys is on a per app level and is not affected by what other apps the user has installed on their phone.
There are two GCM limits according to GCM documentation. One is that the GCM server can hold up to 100 messages while device is offline.
The other one is that there can only be up to 4 collapse keys PER SENDER_ID PER DEVICE.
In our use case, we can have 5 or more different apps (each one is an APK) installed on a single device and each of them registers and obtains a GCM registration id independently (using same SENDER_ID though, as we only have one Google project on server side), does each app have its own 100/4 quota or all 5 apps share the same 100/4 quota?
If all 5 apps share the same 100/4 quota, then it is very likely that after then phone gets back on line (from sleep), one or some apps will not be able to receive any queued GCM message, in the case when there are more than 100 messages queued on GCM server (hence all deleted, leaving a single special "you-need-to-perform-full-sync" message, to which app out of 5?) or the 4-collapse-key limit kills the 5th collapse-able message.
I believe the 100/4 quota is per Registration ID (which means separate quota for each app, regardless of whether or not you are using the same sender ID for the different apps).
The documentation doesn't say that the 100/4 quota is per sender ID. It says it is per device. However, since it doesn't make sense that all the apps on a single device will share the same quota, I assume that the quota is per application per device (i.e. per Registration ID).
From the Android Developer Documentation,
GCM only allows a maximum of 4 different collapse keys to be used by the GCM server per >device at any given time.
You can specify as many collapse keys as you want. But if a message is not push to the device yet, the maximum unique collapse key stored by GCM will be 4.
The limit on GCM 4 collapse_keys and per 100 messages is per Registration ID so
the other apps will not affected.
Google note about GCM "There is a limit on how many messages can be stored without collapsing. That limit is currently 100. If the limit is reached, all stored messages are discarded. Then when the device is back online, it receives a special message indicating that the limit was reached. The application can then handle the situation properly, typically by requesting a full sync."
so how to my application should requesting a full sync?
This is completely up to you since you need to sync with third party server but
not with the GCM server.
You can follow the same procedure that how you would sync your mobile app with your server.