Sunday, April 20, 2014

Google Cloud Messaging(GCM) for Android. (Part 1/2)

1. What is GCM??

You can implement push service in you app with GCM and can find more information from this page.
This logo image is free to use.
http://developer.android.com/google/gcm/index.html

Under my experience, push messages cannot be delivered well when  user turn on wifi connection. So, you should implent pull service such as alarm notification together or develop your own push service.

The gcm message can be done under below messaging processes.

a. Register for GCM(Client)
This step only occurs on time for each client. In this step, client android app will obtain registration id from GCM connection server.
You can send the reg id to server to let server can send message to client with the key.

b. Send message to GCM connection server.(Server)
You have to implement a program to send message to GCM.
You will use the reg id of step 1.
I'll choose HTTP JSON method for this post.

c. GCM connection server tries to send message to client app.

d. Receive GCM message.(Client)
You have to implement WakefulBroadcastReceiver in your app.

2. Steps for implementation.
The step is quite simple.
  • Creating a Google API project and obtaining an API Key.
  • Set Up Google Play Services for Android.(Client)
  • Implement GCM register code.(Client)
  • Implement GCM receiver code.(Client)
  • Implement GCM message sender.(Server)
3. Creating a Google API project and obtaining an API Key.
  • go to google cloud console(https://cloud.google.com/console).
  • Create project if you don't have on yet. Copy project code to use later.
  • go to APIs & auth>Credentials and click 'Create New Key' button under Public API access.
  • In the Create a new key dialog, click Browser key.
  • When click 'create' button you can have a new browser key. Copy it to further use.
4. Set Up Google Play Services for Android.(Client)

Please see How to use Android Google Maps(API Ver2) in older Android version such as 2.3.(Part 1/2)  for this topic.
 
5. Implement GCM register code.(Client)
Your PROJECTCODE - the google API project code you created in 3. Creating a Google API project and obtaining an API Key.

@Override

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_gcm_register);


   RegisterBackground task = new RegisterBackground();

   task.setContext(getBaseContext());

   task.execute();

}
// inner class that sends GCM registration request and receive registration id.
class RegisterBackground extends AsyncTask<String,String,String>{
   Context ctx;
   public void setContext(Context ctx){
      this.ctx = ctx;
   }
@Override

public String doInBackground(String... arg0) {
   String msg = "";
   String errMsg = "";
   try {
      String regId = GoogleCloudMessaging.getInstance(ctx).register("Your PROJECTCODE");
      msg = "Dvice registered, registration ID=" + regId;
      Log.i("GCMRegister.RegisterBackground", msg);
      if(regId != null){
         sendMyInfo(regId);
      }
   } catch (IOException ex) {
      errMsg = "Error :" + ex.getMessage();
      Log.e("GCMRegister.RegisterBackground", errMsg, ex);
   }
   return errMsg;
}
}
 
public void sendMyInfo(String regId){
    // TODO add your code to send GCM registration id to your server system.


No comments:

Post a Comment