Keep Google Cloud Messaging Reliable
The purpose of the article is to familiarize yourself with the most common pitfalls in working with the notification service from Google.
The source was a very useful, in my opinion, article Keeping Google Cloud Messaging For Android Working Reliably from the developers of Pushbullet , a convenient application for synchronizing notifications between Android devices and the Chrome browser.
I hope all of you have already read the articles on Google Cloud Messaging:
This point is completely ignored in the official documentation of the Implementing GCM Client . Thus, this is one of the most important details that you need to know when registering for GCM.
Call gcm.register (SENDER_ID); very often leads to an error with the exception of IOException "SERVICE_NOT_AVAILABLE".
It is likely that it will never happen on your test devices and may not be ready for it. This can lead to a very negative effect, since the reliability of your application can seriously suffer, at the moment when your application is already published, and it happens very often.
How to be It is pretty simple. This message means that GCM was unable to register and you need to try again. Documentationin this case, it recommends exponentially wait for time and try again, this is excellent advice.
This advice is rather strange and may not be relevant, but since there is no way to confirm that the error in GCM has been fixed, we will also provide for this.
The error is this:
It doesn’t matter how many times you call gcm.register (SENDER_ID); it constantly crashes with an error on some devices. Even if the working registration ID was successfully created, but was never returned as a result of the function.
To get this registration ID, register your GCM BroadcastReceiver for the following event:
You will get the opportunity to get a working registration ID:
In case this variable is not null and not empty, you will get a working registration ID. Further, you can save it and transfer it to your server as you usually do.
This tip is taken from the official documentation , but it is very easy to skip, so do not forget about it.
It is unlikely that the user will open your application immediately after updating from the market, so it is very important to register the application again automatically. It is very important to keep the messaging service operational immediately after the update.
The best way to activate the automatic registration process is to declare a BroadcastReceiver class in AndroidManifest that will listen to the PACKAGE_REPLACED event :
The moment the onReceive method is called, you can initiate GCM registration. Next, replace the current registration ID with a new one and send it to your server.
This is not documented anywhere (as far as I know), but it is known that registration ID depends on the Android device ID.
Which can change if the firmware of the device changes or the user makes a reset to the factory settings (factory reset). Thus, you must register the device again if there is a change in Android ID.
As in the case of updating the application, the user is unlikely to immediately launch your application, which could initiate the GCM registration process at startup. You must take care of the automatic re-registration of the device in order to ensure that the messaging service works without waiting for the user to start the application.
What is the best way to do this? There is only one way to do this, although this method is not ideal. You need to register your application whenever the phone restarts. To do this, give the application in AndroidManifest a new permission:
And set BroadcastReceiver to receive the BOOT_COMPLETED event :
Now, every time you call onReceive this listener, you can register your device with GCM again.
Perhaps it would be more correct to call device registration only if the Android ID has really changed. The important thing is that with the help of this seemingly optional work, we would be sure that the device was registered even when it was probably not necessary, because we can’t be 100% sure that the Android ID will definitely change.
That's all. It’s sad that in order to ensure the smooth operation of GCM, I had to go to such “dances with a tambourine” and even sadder that most of these points are not documented by Google itself.
Errors and typos in PM
The source was a very useful, in my opinion, article Keeping Google Cloud Messaging For Android Working Reliably from the developers of Pushbullet , a convenient application for synchronizing notifications between Android devices and the Chrome browser.
I hope all of you have already read the articles on Google Cloud Messaging:
- We are writing an application for Android with support for Cloud to Device Messaging (C2DM)
- GCM - Google’s New Push Notification Service
- Learn more about implementing GCM support on an Android client.
Lesson # 1: Get Ready to Receive SERVICE_NOT_AVAILABLE Frequently
This point is completely ignored in the official documentation of the Implementing GCM Client . Thus, this is one of the most important details that you need to know when registering for GCM.
Call gcm.register (SENDER_ID); very often leads to an error with the exception of IOException "SERVICE_NOT_AVAILABLE".
It is likely that it will never happen on your test devices and may not be ready for it. This can lead to a very negative effect, since the reliability of your application can seriously suffer, at the moment when your application is already published, and it happens very often.
How to be It is pretty simple. This message means that GCM was unable to register and you need to try again. Documentationin this case, it recommends exponentially wait for time and try again, this is excellent advice.
Lesson # 2: Get Ready for Repeated Register Call Errors Even If a Worker Registration ID Is Created
This advice is rather strange and may not be relevant, but since there is no way to confirm that the error in GCM has been fixed, we will also provide for this.
The error is this:
It doesn’t matter how many times you call gcm.register (SENDER_ID); it constantly crashes with an error on some devices. Even if the working registration ID was successfully created, but was never returned as a result of the function.
To get this registration ID, register your GCM BroadcastReceiver for the following event:
You will get the opportunity to get a working registration ID:
final String registrationId = intent.getStringExtra("registration_id");
In case this variable is not null and not empty, you will get a working registration ID. Further, you can save it and transfer it to your server as you usually do.
The two tips above will help you make sure your devices are successfully registered.
The next two tips will tell you when you need to register your device again and how to do it effectively in order to maintain the continuous operation of your message service.
Lesson number 3: Make sure that you register your device again whenever you update your application
This tip is taken from the official documentation , but it is very easy to skip, so do not forget about it.
It is unlikely that the user will open your application immediately after updating from the market, so it is very important to register the application again automatically. It is very important to keep the messaging service operational immediately after the update.
The best way to activate the automatic registration process is to declare a BroadcastReceiver class in AndroidManifest that will listen to the PACKAGE_REPLACED event :
The moment the onReceive method is called, you can initiate GCM registration. Next, replace the current registration ID with a new one and send it to your server.
I hope you remember what to callIt’s not worth it directly on onReceive BroadcastReceiver, since this method will be called in the main thread of the application (MAIN_THREAD), and should not work directly with the network.gcm = GoogleCloudMessaging.getInstance(activity); regid = gcm.register(SENDER_ID);
For these purposes, it is recommended to use AsyncTask , or a specially created background service, or Thread at worst.
Lesson number 4: Make sure that you register your device again if you update the version of Android on the device
This is not documented anywhere (as far as I know), but it is known that registration ID depends on the Android device ID.
Which can change if the firmware of the device changes or the user makes a reset to the factory settings (factory reset). Thus, you must register the device again if there is a change in Android ID.
As in the case of updating the application, the user is unlikely to immediately launch your application, which could initiate the GCM registration process at startup. You must take care of the automatic re-registration of the device in order to ensure that the messaging service works without waiting for the user to start the application.
What is the best way to do this? There is only one way to do this, although this method is not ideal. You need to register your application whenever the phone restarts. To do this, give the application in AndroidManifest a new permission:
And set BroadcastReceiver to receive the BOOT_COMPLETED event :
Now, every time you call onReceive this listener, you can register your device with GCM again.
Perhaps it would be more correct to call device registration only if the Android ID has really changed. The important thing is that with the help of this seemingly optional work, we would be sure that the device was registered even when it was probably not necessary, because we can’t be 100% sure that the Android ID will definitely change.
That's all. It’s sad that in order to ensure the smooth operation of GCM, I had to go to such “dances with a tambourine” and even sadder that most of these points are not documented by Google itself.
Errors and typos in PM