At the 2012 Google IO conference, Google announced the release of Google Cloud Messaging, AKA GCM, as a replacement for its Cloud to Device Messaging (C2DM) protocol. Being new to cloud messaging, I was glad to have the chance to start with something fresh, knowing that I did not have to unlearn any earlier protocols to get this under my belt.
But I found very few examples, most of which only gave me partial explanations, so after battling through some basic GCM concepts and actually getting the thing to work, I’ve started putting together this tutorial of the fundamentals.
This tutorial will demonstrate the use of GCM for the broadcast of messages to an Android client from a Tomcat server. It will also make use of sending broadcastintents from a service and receiving those broadcastintents from an app’s activity. The end result will look something like this screenshot, where a web server displays a JSP page for a message to be transmitted, next to a DroidAtScreen capture of a broadcast message.
I’ve uploaded two zip files – one containing the Android client and one containing the Tomcat service. You should be able to download either file, unzip to a folder, and then import the unzipped folder as a project in Eclipse.
NOTE: You’ll need to have an instance of Tomcat configure to work from within your Eclipse environment, or you’ll need to manually compile and deploy a WAR file from the provided source.
NOTE2: You’ll also need to update the Java code in the projects to make use of your PROJECT ID, Android device’s generated registration ID, and the browser key that’s returned when you generate your GCM project in Google’s console.
You will need to have a running instance of Tomcat that you can control through Eclipse. You will also need an Android device, and a Google developer login ID. Finally, you must be running the latest (r20) version of the Eclipse ADT.
Some GCM Fundamentals
I’ll need to put a diagram here.
- A developer creates a Google APIs GCM project, which generates a project id and a “browser” id to be used in the project’s server-based code.
- An Android app registers the device its running on with GCM, using that project id, which generates a registration id. The app should transmit that registration id to the project’s server
- The project’s server code can now send a broadcast message to any registered devices by communicating with the GCM service, using the browser id. For java-based projects, there is a jar file that facilitates this communication.
The GCM Demo – The Big Picture
This project should be built in three steps:
- Create a Google APIs project at the Google portal, and generate and save the project id and API keys.
- Build and run the Android client app, letting it register with GCM, and capture the generated device app key.
- Build the web service, using the browser key to send to GCM, and using the captured device app key to broadcast to your target device
Project Build, More Detail:
You’ll see a lot of steps listed below, which might seem daunting at first. But most of the pre-coding setup steps are quite straightforward, and you should be able to get your projects set up in about 20 minutes before you have to code anything.
Most of the information for steps 1 and 2 was taken verbatim from this
GCM Getting Started guide: