From C2DM to Google Cloud Messaging

July 17, 2012
google cloud messaging push notifications c2dm

(This is an article for Google Cloud Messaging cross posted from the Warply blog)

Google IO is over and we’re left with lots of interesting updates. We saw the new version of Android, Jelly Bean, we used Google Docs with Offline Editing and finally got Chrome and Google Drive to the iPad. The coolest thing of all, though, is what was introduced as Google Cloud Messaging, or simply GCM.

Google, over the past two years, had a service called C2DM, or Cloud to Device Messaging, which had a really big traction, despite being a labs project.

Android Cloud to Device Messaging (C2DM) is a service that helps developers send data from servers to their applications on Android devices. The service provides a simple, lightweight mechanism that servers can use to tell mobile applications to contact the server directly, to fetch updated application or user data. The C2DM service handles all aspects of queueing of messages and delivery to the target application running on the target device.

Since the Google IO 2012, this project got out of the labs after being polished, made simpler and easier to use and was renamed to Google Cloud Messaging.

Well, what’s so great about this thing? Why does it get so much traction? The answer is simple. This service allows a server to send messages asynchronously to devices, making possible what is called the “send to sync” process. Instead of having the application polling the server between several time intervals, the server sends a message to the device, as soon as there is a new update. Then, the application simply requests an update from the server, knowing it exists.

Here are the primary characteristics of Android Google Cloud Messaging:

  • An application on an Android device doesn’t need to be running to receive messages. The system will wake up the application via Intent broadcast when the the message arrives, as long as the application is set up with the proper broadcast receiver and permissions.
  • It does not provide any built-in user interface or other handling for message data. C2DM simply passes raw message data received straight to the application, which has full control of how to handle it. For example, the application might post a notification, display a custom user interface, or silently sync data.
  • It requires devices running Android 2.2 or higher that also have the Market application installed. However, you are not limited to deploying your applications through Market.
  • It uses an existing connection for Google services.

Although the use of this service has many benefits, let’s point out the most important ones. First of all comes battery life. Opening the radio and polling the server is a really battery consuming operation, since the radio does not switch off instantly, leading to up to twenty seconds of battery draining. Since the application no longer needs to poll the server for the updates, it saves both battery and data usage. The application gets also updated in almost real time, since the message processing and sending is done in as little as 4.7ms!

Moreover, you can easily create applications that do not have a normal update interval, i.e. a Twitter client, but need to get updated asynchronously when, let’s say, a new offer comes, just like Warp.Ly applications do. Polling the server, i.e. once per day, until a new offer is available would be a really bad practice, but with C2DM and now GCM, this thing rocks!

Having said all that, let’s concentrate on the changes made to the C2DM service, making Google Cloud Messaging rock so much! The first thing you notice when you start the integration process is that you no longer need to do complicated stuff like getting an auth token, making sure it is always up to date and make everything work (believe me this process was not always working as it should). Now, you just go to the API console, get a server key, put it in your application and that’s it! You are ready to go. After you’ve set up everything, you can now enjoy the new features of GCM, like Message Multicasting (send the same message to multiple devices), Message Collapsing (collapse messages with the same collapse key, delivering only the last one), Multiple Servers for each Application (allowing you to pair an application with different services, let’s say send data related to your application and let Warp.Ly deliver awesome offers) plus many many more! An interesting improvement is the Message Lifetime field, which can vary from 0 to 4 weeks. T his feature allows you to send messages that can be saved in the cloud for as long as you decide if the device is offline and get delivered as soon as the device is available. Setting a Message Lifetime of 0 will send the message if the device is available, or simply discard it, letting you send messages personalized for the exact moment. Apart from that, you can now select to delay the messages if the device is idle, which will treat a device as offline if the device is online but not active. Combine the last two and you get really awesome results, making it possible to maximize the personalized experience.

Since examples are the best way to understand, let’s say that there is a football match. The company sponsoring one of the teams has a mobile application, which has decided to monetize and create traction using Warp.Ly. When the team scores a goal, it is possible to send an offer to all the people having installed the mobile app and are in the pitch using Wap.Ly’s geolocation filtering, lasting just one minute, in order to use the enthusiasm of the scored goal. Since the offer lasts just one minute, there is no need sending it if the user is not active at that moment. Doing this is now possible!

Last, but not least, let’s talk about the implementation. As mentioned before, the integration process is damn easy now! You just go to Google APIs console and enable the Google Cloud Messaging service. You grab the API key and the Project key and you are ready to start sending messages. There is already a Java library available for server side implementation, downloaded alongside with the Android SDK. For the client side implementation, you should also use the given library. The process here is really easy too. You just have to declare some backwards compatible (C2DM) permissions and use the pre made classes to register to the service. In order to listen the messages that have been sent, you just have to extend a special BroadcastReceiver, included in the library and you are set! Let’s not stick to the implementation process, since there is a more detailed guide which can be found here.

Stop waiting, go play with GCM and feel it’s power. If you want to see some stuff in action, check our platform, Warp.Ly and start monetizing your apps with awesome offers. It simply rocks!

comments powered by Disqus