From C2DM to Google Cloud Messaging
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 theGoogle 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!Antonis Kalipetis