Back to top

Android Cloud To Device Messaging --Android demo with DrupalCloud

Recently I updated Android Cloud To Devices Messaging (C2DM) module on Drupal.org. I have write a brief description of how to use C2DM Module in the module's page and its README.TXT file. It describes how our website to build the server-side work of Google Cloud Message Service.

The purpose of this article is to describe how to make our Android projects and properly configured to work with C2DM website. DrupalCloud used here instead of some Drupal Web Services‘ communication and Verification working. Then let us begin right now.

At first, we still need to introduce some necessary preparations to those who is the first time to contact with the development of Google Cloud Message push service.
1, Go to Google APIs Console page and create a new project.
2, After creating project you can see the project id in the url. The project id which will be used as SENDER ID in Android project.
Example: https://code.google.com/apis/console/#<strong>project:56186332XXXX</stro...
3, After that click on Services on the left panel and turn on Google Cloud Messaging for Android.
4, Click on API Access and note down the API Key. This API key will be used when sending requests to GCM server.

Detailed (graphic) reference: http://developer.android.com/google/gcm/gs.html

OK, now we have what we need. Let's begin our Android demo.

First install the required libraries
1, Android Library update your SDK manager to latest version.
2, Goto your Android SDK folder and open SDK Manager and install Google Cloud Messaging for Android Library under Extras section.
3, After installing the library it will create gcm.jar file in your Andoird_SDK_Folder\extras\google\gcm\gcm-client\dist, you need to add this .jar file to your android project.

Creating our Android project
1, Creating project select minimum SDK version API 8.
2, Open AndroidManifest.xml and the following permission are required to make your project support gcm.

  • INTERNET – To make your app use internet services
  • ACCESS_NETWORK_STATE – To access network state (used to detect internet status)
  • GET_ACCOUNTS – Required as GCM needs google account
  • WAKE_LOCK – Needed if your app need to wake your device when it sleeps
  • VIBRATE – Needed if your support vibration when receiving notification

3, Create a class contains the GCM configuration and our server registration url.

    Example: Create a class like "Constants.java" to collect application's constants

4, Create a new class with following functions.

    function to register a user on our server.
    static void register(array[] userInfo, gcm regId)
     
    function to unregister the device.
    static void unregister(gcm regId)

    method to POST data to server
    private static void post(array[] data)
    This step is replaced with DrupalCloud objects here. Of course, you can also take the time to implement it yourself.

5, Key step: Add a new class file handles all GCM related services.

    public class GCMIntentService extends GCMBaseIntentService {
      Method called once the device successfully registered with GCM.
      @Override
      protected void onRegistered() { …… }

      Called when a new message arrived to device.
      @Override
      protected void onMessage() { …… }

      Called when device is unregistered with GCM services.
      @Override
      protected void onUnregistered() { …… }

      On receiving an error
      @Override
      public void onError() { …… }

      Method to generate a notification
      private static void generateNotification() { …… }

    }

6, Create a new xml file under res -> layout folder.
7, Create a new class which will be used to handler user registration.
8, Create a class to handle your push.

  • recive user infomation sent from your register Activity and storing them in static variables.
  • Then checking whether this device has gcm registration id, if not, registering it on gcm by calling GCMRegistrar.register(this, SENDER_ID) method.
  • private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() method will be called when device gets a new push notification message. On simple way, displaying the message on the screen. You might need to take appropriate action on the message depending upon your app requirement. (Example: Storing it in SQLite database)

Others you might want to do
1, Playing custom notification sound.
2, Vibrating device on receiving new notification.
3, Waking up device on receiving new notification.
4, Pushed to N people at the same time.
5, Find someone to push each other.
6, ……
7, ……

Demo produced here have been introduced over the steps. Not too many illustrations and code. I hope that readers can seriously consider every line, and then try to find your own answers, and ultimately finish your own Demo instead of being limited by the Demo I uploaded. Hope it’s useful to you.

Download Address:
https://github.com/Jerenus/C2DeMo
https://github.com/INsReady/DrupalCloud

评论

添加新评论