This topic shows you how to use Windows Azure Mobile Services to send push notifications to an Android app. In this tutorial you add push notifications using the Google Cloud Messaging (GCM) service to the quickstart project. When complete, your mobile service will send a push notification each time a record is inserted.
This tutorial is based on the Mobile Services quickstart. Before you start this tutorial, you must first complete Get started with Mobile Services.
Next, you will use this API key value to enable Mobile Services to authenticate with GCM and send push notifications on behalf of you app.
You mobile service is now configured to work with GCM to send push notifications.
-
In Eclipse, click Window, then click Android SDK Manager.
-
In the Android SDK Manager, expand Extras, check Google Cloud Messaging for Android Library, make a note of the SDK Path, click Install Package, select Accept to accept the license, then click Install.

-
In the Android SDK Manager, under the Android n.m (API x) node, check Google APIs, then click Install.
-
Browse to the SDK path (usually in a folder named adt-bundle-windows-x86_64), and copy the gcm.jar file from the \extras\google\gcm\gcm-client\dist subfolder into the \libs project subfolder, then in Package Explorer, right-click the libs folder and click Refresh.
The gcm.jar library file is now shown in your project.
-
Open the project file AndroidManifest.xml and add the following new permissions after the existing uses-permission element:
<permission android:name="**my_app_package**.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="**my_app_package**.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> -
Add the following code into the application element:
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="**my_app_package**" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" /> -
Note that in the uses-sdk element, the targetSdkVersion must be 16 or greater since notifications don't work for earlier versions of the API.
-
In the code inserted in the previous two steps, replace **my_app_package** with the name of the app package for your project, which is the value of the manifest.package attribute.
-
Open the file ToDoItem.java, add the following code to the TodoItem class:
@com.google.gson.annotations.SerializedName("channel")
private String mRegistrationId;
public String getRegistrationId() {
return mRegistrationId;
}
public final void getRegistrationId(String registrationId) {
mRegistrationId = registrationId;
} This code creates a new property that holds the registration ID.
Note
When dynamic schema is enabled on your mobile service, a new 'channel' column is automatically added to the TodoItem table when a new item that contains this property is inserted.
-
Open the file ToDoItemActivity.java, and add the following import statement:
import com.google.android.gcm.GCMRegistrar;
-
Add the following private variables to the class, where <PROJECT_ID> is the project ID assigned by Google to your app in the first procedure:
private String mRegistationId;
public static final String SENDER_ID = "<PROJECT_ID>";
-
In the onCreate method, add this code before the MobileServiceClient is instantiated:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
mRegistationId = GCMRegistrar.getRegistrationId(this);
if (mRegistationId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} This code get the registration ID for the device.
-
Add the following line of code to the addItem method:
item.setRegistrationId(mRegistationId);
This code sets the registrationId property of the item to the registration ID of the device.
-
In the Package Explorer, right-click the package (under the src node), click New, click Class.
-
In Name type GCMIntentService, in Superclass type com.google.android.gcm.GCMBaseIntentService, then click Finish

This creates the new GCMIntentService class.
-
Add the following import statements:
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
-
In the new class, add the following constructor:
public GCMIntentService(){
super(ToDoActivity.SENDER_ID);
} This code invokes the Superclass constructor with the app SENDER_ID value of the app.
-
Replace the existing onMessage method override with the following code:
@Override
protected void onMessage(Context context, Intent intent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New todo item!")
.setPriority(Notification.PRIORITY_HIGH)
.setContentText(intent.getStringExtra("message"));
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
} Note
In this tutorial, only the onMessage override is implemented. In a real-world app you should consider implementing all four method overrides.
Your app is now updated to support push notifications.
-
Restart Eclipse, then in Package Explorer, right-click the project, click Properties, click Android, check Google APIs, then click OK.

This targets the project for the Google APIs.
-
From Window, select Android Virtual Device Manager, select your device, click Edit.

-
Select Google APIs in Target, then click OK.

This targets the AVD to use Google APIs.
-
From the Run menu, then click Run to start the app.
-
In the app, type meaningful text, such as A new Mobile Services task and then click the Add button.

-
You will see an icon appear in the upper left corner of the emulator, which we have outlined in red to emphasize it (the red does not appear on the actual screen).

-
Click the icon to display the notification, which appears in the graphic below.

You have successfully completed this tutorial.
This concludes the tutorials that demonstrate the basics of working with push notifications. Consider finding out more about the following Mobile Services topics: