google cloud messaging

push notifications

  *  These are  the messages pushed to a central location  & then

      delivered to you.

  * Advantages :

           - No polling concept involved!

           - Better battery life.

                          Data can be sent to the app, even if it is offline:)

           - Live data is provided.

   * Methods are:

            - C2DM

            - GCM

Cloud to Device Messaging(C2DM)


      *  It is a service, that helps developers send data from
          servers to their applications on Android devices.

     *   Requires devices running Android 2.2 or higher that also
          have the play-store installed.

     *   Sends up to 1024 bytes of data.

     *   No guarantees about delivery or the order of messages.

      * C2DM servers will keep running.

     *   This services is now deprecated.
               Stop C2DM! Use GCM!

Google cloud messaging(GCM)

       *  Free service from Google, allows developers to transmit

           data from their servers to their apps.

      *  App doesn't  need to be in running state to receive  data.

      *  Devices:

              - Supports devices with Android 2.2 having Google Play

                store application installed.

             -  Pre-3.0 devices requires Google account.  It is not a 

                requirement on devices running Android 4.0.4 or higher.

gcm features

               - Messages with Payload

                         . Payload up to 4K bytes per message.

                         . Up to 100 messages are stored by GCM.

              - Message multicasting

                          . One request, many devices.

                          . Up to 1000 devices simultaneously.

              - Time to live

              - Multiple senders for social updates


GCM Architecture


       a)  Whenever push notification is needed, our server sends
               message to GCM server along with device registration id.


       b) GCM server will delivers that message to respected
              mobile device using device registration id.

        *   To consume GCM service, first we must get following      


                   - Project Id

                   - API Key

getting project id

       * Open the Google APIs Console page.


       * Click Create project. Browser URL will change to
          something like:  


obtaining an api key

        * In the main Google APIs Console page, select API Access.

       * Click Create new Server key.



      *  Click create

      *  To enable the GCM Service:

               -  Turn the Google Cloud Messaging toggle to ON.


      *  Next, install helper libraries:

                - Google Cloud Messaging for Android Library

                - Ant 1.8

                - Running web server like Tomcat 6 or Jetty, etc


Android coding part

     * Copy the 'gcm.jar' file into the application classpath.

     * Define following permissions in the manifest file:

        <uses-sdk android:minSdkVersion="8"

                 android:targetSdkVersion="xx" />


                 android:name="android.permission.INTERNET" />-


            android:name="android.permission.GET_ACCOUNTS" />


           android:name="android.permission.WAKE_LOCK" />-



   * Add the following broadcast receiver:

<receiver android:name="" 
android:permission="" >
<action android:name="" />
<action android:name="" />
<category android:name="my_app_package" />


      *  Add the following intent service:

      <service android:name=".GCMIntentService" />


      *  Override following methods in GCMIntentService class:

             - onRegistered(Context context, String regId);

             - onUnregistered(Context context, String regId);

             - onMessage(Context context, String regId);

             - onError(Context context, String regId);

             - onRecoverableError(Context context, String regId);


    * Add the following import statement in your application's

      main activity:


    * In the onCreate() method, add the following code:


final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");

server-side application

    *  Copy the 'gcm.jar' file into the server classpath.


    *  Create a servlet class to register the registration IDs.

    *  Create a servlet class to unregister the registration IDs.

    *  Use class to send a

       message to registration ID. For example,


Sender sender = new Sender(myApiKey);
Message message = new Message.Builder().build();
MulticastResult result = sender.send(message, devices, 5);

Demo application

  * After configuring server side using Tomcat & Ant:

  * After running 'gcm-demo-client' application:


    * Now go back to your browser and refresh the page. It will

       show that one device registered:

   *  Click on Send Message. The browser should show:


       * And in your emulator:


google cloud messaging

By Torry Harris Business Solutions