onsdag 20. februar 2013

GCMUtils - utility classes for Google Cloud Messaging

During Google IO 2012 Google updated their push messaging service (from C2DM), releasing Google Cloud Messaging. GCM includes features such as:
  • Ease of use, no sign-up forms
  • Battery efficiency
  • Statistics available through the Developer Console
  • Rich set of new APIs

While C2DM was a really cool technology, it was somewhat complicated to get implemented correctly. GCM have improved this by providing a much simpler API. Let me show a few examples.

These two lines of code provides verification for the device (that it supports push messaging) and the Manifest-file:
 GCMRegistrar.checkDevice(context);  
 GCMRegistrar.checkManifest(context);  

To get the registration id, code similar to this example can be used:
 String regId = GCMRegistrar.getRegistrationId(context);  
 if ("".equals(regId)) {   
   GCMRegistrar.register(context, SENDER_ID);   
 } else {   
   Log.i("Already registered, regId:" + regId);   
 }   

For more information, go to the official GCM page.

As shown above, the GCMRegistrar-class contains several useful methods. However, there are aspects that the standard GCM-library does not support.

GCMUtils (https://code.google.com/p/gcmutils)

This is why my new open source project, GCMUtils, was created. This library is a collection of helpful utility classes and methods, built on top of the standard GCM API. It is created to be used in combination with GCM.

It provides features such as:
  • Registration id handling 
 GCMUtils.getAndSendRegistrationId(this);  
  • Extended verification 
 GCMUtils.checkExtended(this);  
  • Simple configuration 
 # Required  
 receiver-url=  
 sender-id=  
 # optional  
 # values: enabled/disabled  
 check-extended=enabled  
  • Alternative base intent service with more functionality 
 public class GCMIntentService extends GCMUtilsBaseIntentService {  
  @Override  
  protected void onMessage(Context context, String msg) {  
   Log.i(TAG, "Message received: " + msg);  
  }  
  @Override  
  protected void onError(Context context, String error) {  
   Log.e(TAG, "onError: " + error);  
  }  
 }  

Adding it to your project is easy (copy 2 jar files) and it is created to make to development of GCM apps simpler and more efficient. In my own project this has been a big help, and hopefully this will be useful for others as well.

For more information, go to the features page and the quick start guide.

It is still early in the development of this project, so all feedback is appreciated. There are more features planned to get implemented before a final release is ready.