[This post is by Dave Burke, who's an Engineering Manager 80% of the time. — Tim Bray]
Android Cloud to Device Messaging (C2DM) was launched recently as part of Android 2.2. C2DM enables third-party developers to push lightweight data messages to the phone. C2DM created a nice opportunity for us to pull together different Google developer tools to create a simple but useful application to enable users to push links and other information from their desktop / laptop to their phone. The result was Chrome to Phone – a 20-percent time project at Google.
Chrome to Phone comprises a Chrome Extension, an Android Application, and a Google AppEngine server. All of the code is open sourced and serves as a nice example of how to use C2DM.
The message flow in Chrome to Phone is fairly typical of a push service:
The Android Application registers with the C2DM service and gets a device registration ID for the user. It sends this registration ID along with the user’s account name to the AppEngine server.
The AppEngine server authenticates the user account and stores the mapping from account name to device registration ID.
The Chrome Extension accesses the URL and page title for the current tab, and POSTs it to the AppEngine server.
The AppEngine server authenticates the user and looks up the corresponding device registration ID for the user account name. It then HTTP POSTs the URL and title to Google’s C2DM servers, which subsequently route the message to the device, resulting in an Intent broadcast.
The Android application is woken by its Intent receiver. The Android application then routes the URL to the appropriate application via a new Intent (e.g. browser, dialer, or Google Maps).
An interesting design choice in this application was to send the payload (URL and title) as part of the push message. A hash of the URL is used as a
collapse_key to prevent multiple button presses resulting in duplicate intents. In principle the whole URL could have been used, but the hash is shorter and avoids unnecessarily exposing payload data. An alternative approach (and indeed the preferred one for larger payloads) is to use the push message service as a tickle to wake up the application, which would subsequently fetch the payload out-of-band, e.g. over HTTP.
The code for Chrome to Phone is online. Both the AppEngine and Android Application include a reusable package called com.google.android.c2dm that handles the lower-level C2DM interactions (e.g. configuration, task queues for resilience, etc).
Chrome to Phone is useful, but maybe it’s most interesting as an example of how to use Android C2DM.
View the original article here