commerce & mobile solutions
blog snowdog_bold_b

Dealing with SERVICE_NOT_AVAILABLE – Google Cloud Messaging

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

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

After setting up the project and implementing GCM using Google tutorial : you’re most likely to encounter few additional issues.

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

It’s a common problem, even though you have 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:

3. Now we have working Registration ID, even though the IOException SERVICE_NOT_AVAILABLE is thrown on random devices (for random, undocumented reasons).

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 actual time when user starts the app after the update (it’s where the re-registration process takes place) we may get a blank spot,
where push messages stopped working.
2. 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 device in the GCM service.

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

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

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

Then add this to 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 above scenarios, we will have reliably working GCM service in our app. Without the SERVICE_NOT_AVAILABLE exception and without any GCM service interruptions.