2013年11月14日 星期四

Android: Implementing GCM Client for Android

If you have no any idea about the topic, you can try to study this article - Introduction for GCM and Flurry.

1. Implementation Steps
The session is divided into two parts, GCM Mechanism for Android as well as Flurry Mechanism. There are two types on Implementation steps for GCM. The first method is deprecated by Google. The other way is the newest implementation method.

1.1. GCM Mechanism for Android
In the beginning, the session will introduce one thing about implementing GCM mechanism. It is the newest way using “google_play_service lib” project. Another way is an older method which Google has not suggested, but it will not be launched in the document. By the way, both ways are the same on applying for an API key, and only implementing GCM Client is different. The following diagram is a work flow from applying for an API key to implementing a GCM client. The following contents are going to describe the both steps.


1.1.1. Applying for an API Key
  • A. Creating a Google API project
    •  Open the Google Developers Console.
    •  If you haven't created an API project yet, click Create Project.
    •  Supply a project name and click Create.
    •  Copy down your project number.
    •  In the sidebar on the left, select APIs & auth.
    •  In the displayed list of APIs, turn the Google Cloud Messaging for Android toggle to ON.
  • B. Obtaining an API Key
    •  In the sidebar on the left, select APIs & auth > Credentials.
    •  Under Public API access, click Create new key.
    •  In the Create a new key dialog, click Server key.
    •  In the refreshed page, copy the API key. You will need the API key later on to perform authentication in your application server.
  • C. Set Up Google Play Services SDK
    •  Set Up a Project that Uses Google Play Services
    •  Using Eclipse or another IDE:
    •  Setting up “google-play-services_lib” Project

1.1.2. Implementing GCM Client
  • A. Edit Your Application's Manifest
    •  You should add the following contents into the Manifest in Android project, as the following diagram.




  • B. Create a simple layout
    • This layout is a simple view which shows a general bottom used to register device to server. Once we register device success, the button will be disabled, as the following diagram.



  • C. Create a GcmBroadcastReceiver.java
    • The GcmBroadcastReceiver needs to extend the WakefulBroadcastReceiver class. A WakefulBroadcastReceiver is a special type of broadcast receiver that takes care of creating and managing a partial wake lock for your app. It passes off the work of processing the GCM message to a Service (typically an IntentService), while ensuring that the device does not go back to sleep in the transition. If you don't hold a wake lock while transitioning the work to a service, you are effectively allowing the device to go back to sleep before the work completes. The net result is that the app might not finish processing the GCM message until some arbitrary point in the future, which is not what you want. If you want to know how to implement this class, you can refer to the sample code.
  • D. Create a GcmIntentService.java
    • The intent service does the actual work of handling the GCM message. When the service is finished, it calls GcmBroadcastReceiver.completeWakefulIntent() to release the wake lock. The completeWakefulIntent() method has as its parameter the same intent that was passed in from the WakefulBroadcastReceiver. If you want to know how to implement this class, you can refer to the sample code.
  • E. Create a RegisterApp.java
    • This class should extend AsyncTask class. We need to complete some important tasks within the class. You should send the registration ID to your server over HTTP, so it can use GCM/HTTP or CCS to send messages to your app. The request to your server should be authenticated if your app is using accounts. You need to pay attention to some things about how to correctly finish these tasks. Setting your “Sender ID” and “URL” in this file is the most principal thing, as the following diagram.


2. References








沒有留言:

張貼留言