Push messaging is one of the most important features of today's apps. It enables server-app communication, based on the broadcast system. The ability to inform the user about upcoming updates, and changes, or simply to engage them more in the app experience, is crucial for successful app development.

To make sure that Google Cloud Messaging runs smoothly, without any unexpected behavior, a few steps are necessary to be taken.

After setting up the project and implementing GCM using the Google tutorial: http://developer.android.com/google/gcm/client.html you’re most likely to encounter a few additional issues.

I. Everything is set up perfectly, but SERVICE_NOT_AVAILABLE keeps on coming when trying to get the Registration ID

It’s a common problem, even though you have a proper GCM setup. The SERVICE_NOT_AVAILABLE issue comes in pair with the call to register method, where we should get the Registration ID, but instead, we get this error.

The workaround for it is simple, all we have to do is follow these steps:

1. Add REGISTRATION to the GcmBroadcastReceiver Intent Filter in AndroidManifest.xml file, so it will look like this:

  

2. Obtain the Registration ID in GcmBroadcastReceivers onReceive method:

  

II. Keep track of the Registration ID, as it may change on various conditions. Basically, we need to re-register every device with GCM, when:

1. App gets an update. Unfortunately between the app update and the actual time when the user starts the app after the update (it’s where the re-registration process takes place), we may get a blank spot, where to push messages stopped working.

2. The above is also true for the Android System update.

So we need to take care of both scenarios (App update and System update). Lucky us, all we have to do is register receivers for these particular system events, and run the re-registration process once they occur. Once Receivers onReceive method is triggered, we know it’s time to register again the device in the GCM service.

To handle the first case – app update – add this to the application tag in the manifest:

  

And create a relevant UpdateReceiver class (that extends standard BroadcastReceiver).

Second case – the system update: First, add this permission to the manifest:

  

Then add this to the application tag in the manifest:

  

New BootReceiver class (also extending from BroadcastReceiver) also has to be created. In both classes, in onReceive method we need to handle the re-registration process. Removing the stored Registration ID is also recommended, so even if something goes wrong, the app will re-obtain the Registartion ID on consecutive app launch.

After securing the above scenarios, we will have a reliably working GCM service in our app. Without the SERVICE_NOT_AVAILABLE exception and without any GCM service interruptions.