Android: Messages from the Cloud with GCM
One of the Google services which received an update recently, announced at Google I/O 2012, was the Cloud to Device Messaging (C2DM) service. The next iteration has been labelled Google Cloud Messaging (GCM). Along with the moniker change, there are a few key changes between C2DM abnd GCM which add functionality and make it easier to use.
- Multicast messages: You can broadcast messages of up to 1000 registration IDs at the same time from a single request
- Multiple senders: Messages to the same app can be sent by multiple parties thanks to a change in the way registration is handled
- Time-to-live (TTL) messages: Messages can have a TTL value of between 0 to 4 weeks helping to ensure messages about events that expire don’t reach your users after the fact. The GCM servers will throttle the messages to prevent notification spam. Messages that can’t be instantly delivered will be stored by the Google servers until they are delievered or expire, messages with a TTL of 0 aren’t stored anywhere and also aren’t subject to any throttling.
- Payload: messages can bundle up to 4Kb of data to be delivered with the message, up to 100 of these messages can be stored on the GCM servers if they can’t instantly be delivered, after which you’ll receive an error code which your server will have to deal with.
Google states that C2DM will be maintained for moment, but won’t accept any new users nor grant new quotas. They also point out that C2DM and GCM are not interoperable. For example, you cannot post notifications from GCM to C2DM registration IDs, nor can you use C2DM registration IDs as GCM registration IDs. Your server-side application must keep keep track of whether a registration ID is from C2DM or GCM and use the proper endpoint. Fortunately they provide easy to follow instructions as to how to migrate your apps from C2DM to GCM and Google has even provided sample GCM apps (both client and server side) for you to look at (something which was sorely missing with C2DM).
If you haven’t yet used Google’s cloud messaging service, it’s now easier than ever – full instructions on getting started with GCM are given on the developer website. Just remember that it requires devices running android 2.2 (sdk version 8) or later.
Safari Books Online has the content you need
Check out these Android books available from Safari Books Online:
|Android in Action, Third Edition is a fast-paced book that puts you in the driver’s seat–you’ll master the SDK, build WebKit apps using HTML 5, explore cross-platform graphics with RenderScript, learn to use Native Development Kit, and master important tablet concepts like drag-and-drop, fragments, and the Action Bar, all new in Android 3.|
|Beginning Android 4 is fresh with details on the latest iteration of the Android platform. Begin at the beginning by installing the tools and compiling a skeleton app. Move through creating layouts, employing widgets, taking user input, and giving back results.|
|Android in Practice is a treasure trove of Android goodness, with over 90 tested, ready-to-use techniques including complete end-to-end example applications and practical tips for real world mobile application developers. The book dives into important topics like multitasking and services, testing and instrumentation, building and deploying applications, and using alternative languages.|
|Android UI Fundamentals: Develop and Design walks developers through the different choices available on their way to creating a well-designed application for Android. While building a simple application, Jason works through the basics of Android UI development including layout, event handling, menus and notifications.|
|Programming Android shows experienced application developers what they need to program for the Android operating system — the core building blocks, how to put those blocks together, and how to build compelling apps that work on a full range of Android devices.|
|Whether you want to develop a commercial application for mobile devices, or just want to create a mobile mashup for personal use, Android Application Development demonstrates how you can design, build, and test applications for the new mobile market|
About this author
|Martyn Haigh has been a coder since his dad showed him a BBC Micro at the age of 3. He has a degree in Computer Science, likes clean code and has been programming Android devices for longer than it’s been commercially viable. In his spare time he snowboards, slays monsters and has been the Times Magazine person of the year on three different occasions. Sometimes he writes things on www.martynhaigh.com. He’d love you to visit.|