GCM replaces C2DM

Finally! good news from the android team! The old c2dm is deprecated and the new GCM is here:
Same concept but much better features.

I need to say that the best news is that we can finally get push messages as fast as an iphone does. Put simply, we can now send to multiple devices the same push message in one request!!!!



That seems the best of the new apis, it was terribly annoying when we had to push one news entry to 20k android devices: the process took around 4-5 minutes in the perfect case even after optimization. At the same time, we could send the same message to 30k iphones in 5 seconds.

Now, the android team introduces what definitely made my day, GCM! even its name sounds cool.

Anyway, it seems most of the basics are still the same:

REGISTRATION:


  • It is still based on intents and upon firing a registration intent (com.google.android.c2dm.intent.REGISTER), your app will receive an asynchronous broadcast (com.google.android.c2dm.intent.REGISTRATION) which will hand your app a registration ID, aka token. This token is used by your server to send messages to the device
  • I know this is old news but it is important to understand that it is not good practice to send the registration intent every time the app starts (I know people who do this). Actually, you should work on sending the registration intent once and saving the token that you receive in the broadcast receiver and, if you wish to, send it several time to your own push server for no good reason at all!
  • Also, remember that your broadcast receiver's might receive multiple registration intents (onRegistered) and, therefore, your server must have the necessary apis to replace an old token with a new one which of course implies that this function could handle the situation where is it triggered whilst a token already exists (inform the server to replace it)

SENDING A MESSAGE:

  • Your server should have an api key as usual to send the messages. The data sent is still the same old name-value pair format. Now the cool thing is the ability to send to up to 1000 devices per request (using json formatted request):
Example request:
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{ "collapse_key": "score_update",
  "time_to_live": 108,
  "delay_while_idle": true,
  "data": {
    "score": "4x8",
    "time": "15:16.2342"
  },
  "registration_ids":["4", "8", "15", "16", "23", "42"]
}

Notice that the tokens are sent as a json array and that the data is still encoded as a name value pair. collapse_key is still there and it is used to replace any old (unsent) message queued in the google servers having the same collapse_key with this message.

I am just happy and extremely happy that this has been implemented!!! Please check the Official Docs to implement fully. Congratulations everybody!

Related: How to implement GCM PhP Push Server for android

No comments:

Post a Comment