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

continued..


       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      

              credentials:

                   - Project Id

                   - API Key


getting project id

       * Open the Google APIs Console page.

           https://code.google.com/apis/console/

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

        https://code.google.com/apis/console/#project:4815162342











     

obtaining an api key

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

       * Click Create new Server key.














     

continued..

      *  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" />

        <uses-permission

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

       <uses-permission

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

      <uses-permission

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

 

continued..


   * Add the following broadcast receiver:


<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>






continued..

      *  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);


continued..


    * Add the following import statement in your application's

      main activity:

   import com.google.android.gcm.GCMRegistrar;


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

      

   GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
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 com.google.android.gcm.server.Sender class to send a

       message to registration ID. For example,

import com.google.android.gcm.server.*;

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:










continued..

    * 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:














continued..


       * And in your emulator:







THANK YOU

Made with Slides.com