Created: 2012-03-25 20:32
Updated: 2014-06-03 02:22


Cloud to Device Messaging Library

This is just a mavenized, improved version of the C2DM classes used by some demo apps. It is meant to make it easier for any Android developer to implement C2DM in his/her app. Before you use this library, make sure you already have signup to C2DM.


1. Just add this to your POM:


or download the JAR from the Maven Central Repository and link it manually to your project.

2. Create a class that extends com.google.android.c2dm.C2DMBaseReceiver (remember that, since this class is actually an Android Service, you must declare a default constructor; and you must send the registered sender email to the super class. For instance: super("your_registerd_sender_id@gmail.com")). Then modify your manifest to look like this:

<application... >

    <!-- C2DM service and receiver -->
    <service android:name="com.your.package.name.YourReceiverImplementation"/>
    <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
        <meta-data android:name="com.google.android.c2dm.implementation"
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <category android:name="com.your.package.name"/>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
            <category android:name="com.your.package.name"/>


<permission android:name="com.your.package.name.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="com.your.package.name.permission.C2D_MESSAGE"/>

3. When necessary, register the device using C2DMessaging.register(String) method. For instance:

// this can be placed in your home activity.
String registrationId = C2DMessaging.getRegistrationId(this /**context**/);
if (TextUtils.isEmpty(registrationId)) {
    C2DMessaging.register(this, "your_registerd_sender_id@gmail.com");

Do not forget to send the registration ID to your server from the onRegistration method of your com.google.android.c2dm.C2DMBaseReceiver implementation. You need that ID to send push notifications to the clients.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more