dexxtr blog

Web, mobile and other strangeness around me.
Recent Tweets @dexxtr
Posts I Like

In July 3, 2012 Google announced a Google Cloud Messaging for Android. How to use this service you can find tutorial on official developers page. It’s realy helpful guide and doesn’t apear any questions, thanks Google :) 

By default we should use GCMIntentService class for handle incoming messages, registration or unregistration service and other things. And also that class should be in root application package. If we changed package or class name (eg. com.dexxtr.gcm.GCMService) we can see in logs message like this:

07-27 00:50:21.034: V/GCMBroadcastReceiver(23422): GCM IntentService class: com.dexxtr.GCMIntentService

and our service class doesn’t receive any message. What’s wrong? Google reference describes:

This intent service will be called by the GCMBroadcastReceiver (which is is provided by GCM library), as shown in the next step. It must be a subclass of com.google.android.gcm.GCMBaseIntentService, must contain a public constructor, and should be named my_app_package.GCMIntentService (unless you use a subclass of GCMBroadcastReceiver that overrides the method used to name the service).

What should we do? We should create receiver and set the name of our custom service to overrided method getGCMIntentServiceClassName.

public class GCMReceiver extends GCMBroadcastReceiver { 
    @Override
	protected String getGCMIntentServiceClassName(Context context) { 
		return "com.dexxtr.gcm.GCMService"; 
	} 
}

And don’t forget change receiver name in AndroidManifest.xml to

That’s it. Run the application and see in LogCat string like this:

07-27 00:56:36.059: I/GCMService(23489): Device registered: regId= APA91bFZEteUc6DJM3hrQ2BX6Yti0vz.......

Here you can download sample project: GCM-IntentService-Name. You only should set your Project ID and your GCM service class name in strings.xml.