Smartphones

Implementing Google's Cloud to Device Messaging

Kyle Miller assures Android developers that the time it takes to enable Google's powerful Cloud to Device Messaging technology is well worth it.

Push notifications allow you to send a message from a server to a device, without the device having a connection to the server sending the message. These notifications were introduced to Android in version 2.2 (Froyo) via Google's Cloud to Device Messaging (C2DM). This framework is not meant for sending large amounts of data, but rather is best used to tell a device to call home to the server and fetch whatever information is needed. I'll walk you through the process of registering for and implementing C2DM in your Android application (and server-side). Sample code is also provided at my GitHub repository.

Register for access to C2DM

First, you must request permission to be able to send push notifications from Google. You will need a Google account, and it is highly recommended that you create a new one rather than use one of your existing accounts. You may register for access here.

One of the required fields when registering is your application's package name. I will let you in on a little secret I discovered: This package name does not seem to be a binding contract. That is, I've been able to send push notifications to other applications with different package names that used the same Google account to get a registration ID (more on this later).

Set up your Android application

Three basic things need to be in place in order for your Android application to be able to register for and receive push notifications:

  • Modifications to your Manifest XML file
  • C2DMReceiver Broadcast Receiver
  • Code to register for C2DM
Manifest.xml

In your Manifest XML file, you need to add permissions and then use them. For the sake of this sample, the permission I created was "com.teleknesis.c2dm.sample.permission.C2D_MESSAGE":

<permission android:name="com.teleknesis.c2dm.sample.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.teleknesis.c2dm.sample.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

Next, you need to add a node for the C2DMReciever BroadcastReceiver:

<receiver android:name="com.teleknesis.c2dm.sample.C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">

<!-- Receive the actual message -->

<intent-filter>

<action android:name="com.google.android.c2dm.intent.RECEIVE" />

<category android:name="com.teleknesis.c2dm.sample" />

</intent-filter>

<!-- Receive the registration id -->

<intent-filter>

<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<category android:name="com.teleknesis.c2dm.sample" />

</intent-filter>

</receiver>
C2DMReceiver

In the sample code provided, you will see the C2DMReceiver class; this class is responsible for receiving your registration information as well as the messages sent from your server. The code is fairly straight-forward, but the main thing to note is that after you receive your registration ID, you should send it to your server to be stored and used later to send notifications. I have also included a broadcast intent afterwards to let the Main activity know the registration process is complete.

Code to register for C2DM

The code that kicks off the registration process is located in Main.java. We create an Intent, put extras regarding our account information, and then start a service with the intent. Make sure to replace "<YOUR_EMAIL>" with the email address used to sign up for C2DM:

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");

registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate

registrationIntent.putExtra("sender", "<YOUR_EMAIL>");

startService(registrationIntent);

Server-side implementation

The basic concepts to understand server-side are:

  • Your server needs a Google Login key for the Google account you used to sign up for C2DM.
  • The device's Registration ID is used in conjunction with your Google Login key to send a push notification.

Inside the "assets" folder of the sample Android application provided, you will see "SendNotifcation.php"; you may use this file as a reference to create your own script, but I will also walk you through the basic workflow of the server.

  1. Check for the presence of a stored Google Login key (I store mine in MySQL). If no key is present, this means you need to request one from Google.
  2. If necessary, request a Google Login key and store it (you should store this value so you don't have to re-request it every time).
  3. Attempt to send a push notification using your Google Login key and the Registration ID of the device you want to push to. This may fail if your Google Login key is expired (which will happen periodically), so check for this error, and if necessary request a new Google Login key (and store it). Then, attempt to send the push notification again.

Conclusion

It may take you several hours to get this entire mechanism in place (especially the first time), but once you do, you'll be very glad you took the steps to enable this incredibly powerful technology. Hopefully the sample code provided will be easy enough to understand and follow, but if you have any questions, please feel free to include them below in the comments.

About

Kyle is a senior software engineer specializing in web/Android development living in Austin TX. He's a self-professed "gadget freak" whose passion for mobile devices drove him to jump into the mobile industry in 2010. He enjoys the fast-paced nature ...

0 comments

Editor's Picks