I am in the process of writing my first Android application and this involves using Google's Cloud to Device Messaging (C2DM) Service. The basic goal of that service is to send messages from the Google Cloud to a device and have the appropriate application respond to the message, even when not currently running. This is a newer feature in Android as of version 2.2. Because this is new there are not many tutorials or examples out there so I'm posting what I did to get this working. This is not necessarily the best way, just what worked for me.
1) You definitely need to familiarize yourself with the C2DM reference site: http://code.google.com/android/c2dm/index.html.
2) You will also need to sign up for access: http://code.google.com/android/c2dm/signup.html
This was almost instant for me, but this is still in beta as I understand it so there may be a wait when you try it
That's all there is to getting started. From there on out it's all code. I have included a downloadable package that I used for my project. I found reference to C2DM classes that Google wrote but I cannot find where I found them so sorry that I can't reference that blog/site. Here's that package:
com.google.android.c2dm
You will then need to create a class to extend the C2DMBaseReceiver class from the above downloadable package. Here's a starting point for that code:
package your.package;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.google.android.c2dm.C2DMBaseReceiver;
public class C2DMReceiver extends C2DMBaseReceiver {
public C2DMReceiver() {
super(Model.getInstance().senderEmail);
}
@Override
public void onRegistered(Context context, String registrationId)
throws java.io.IOException {
// SEND REGISTRATION TO WEB SERVER
};
@Override
protected void onMessage(Context context, Intent intent) {
Log.e("YOUR_TAG", "Message: Fantastic!!!");
// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null) {
// DO SOMETHING WITH THE MESSAGE
}
}
@Override
public void onError(Context context, String errorId) {
Log.e("YOUR_TAG", "Error occured!!!");
}
}
Now, in your application code, you need to use the C2DMessaging class to do the registering and un-registering. Here's code fro that:
Here is registering with C2DM
C2DMessaging.register(this, "YOUR_SENDER_EMAIL@gmail.com");
Here is un-registering with C2DM
C2DMessaging.unregister(this);
That's all the code needed to get this running. Finding that out took a while for me. The last thing you need to do is get your AndroidManifest.xml file set up correctly. Here's what you need to add to get this working (and this is basically straight out of the C2DM reference site):
Add this inside your <application> node:
<service android:name=".C2DMReceiver" />
<!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it -->
<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" 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="YOUR.PACKAGE.PATH" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="YOUR.PACKAGE.PATH" />
</intent-filter>
</receiver>
Add this outside your <application> node:
<!-- Only this application can receive the messages and registration result -->
<permission android:name="YOUR.PACKAGE.PATH.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="YOUR.PACKAGE.PATH.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Send the registration id to the server -->
<uses-permission android:name="android.permission.INTERNET" />
The "YOUR.PACKAGE.PATH" should be replaced with whatever your package attribute is specified in your manifest node:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="YOUR.PACKAGE.PATH"
android:versionCode="1"
android:versionName="1.0.0">
I hope this helps people get started in C2DM faster than I was able to get started. Overall, I have to applaud Google on the Android framework. This is my first real taste of it and I'm looking forward to writing more apps in the future.