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