As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019. Migrate GCM apps to Firebase Cloud Messaging (FCM), which inherits the reliable and scalable GCM infrastructure, plus many new features. See the migration guide to learn more.

Registration state sync handling

There are two scenarios that require special care when using register():

Client app update

Backup and restore

Client app update: When a client app is updated, it should
invalidate its existing registrationID, as it is not guaranteed to work with
the new version. The recommended way is to store the current app version
when a registration ID is stored. When the client app starts, compare the
stored value with the current app version. If they do not match, invalidate
the stored data and start the registration process again.

Backup and restore: You should not save the registration ID when a client app is
backed up. This is because the registration ID could become invalid by the time
the client app is restored, which would put the client app in an invalid state
(that is, the app thinks it is registered, but GCM does not
store that registration ID anymore—thus the app will no longer get
messages). The best practice is to initiate the registration process as if the app has been
installed for the first time.

Receiving messages from multi-senders

To receive messages from multi-senders, a client app calls
register() with all the app server’s senderIDs, separated by commas.

Implementing register() in a client app

In the
following snippet the onCreate() method in the sample app's
main activity checks to see if the app is already registered with GCM and with
the server:

If the registration ID doesn't exist or the app was updated,
getRegistrationId() returns an empty string
to indicate that the app needs to get a new registration ID. getRegistrationId() calls
the following method to check the app version:

If there isn't a valid existing registration ID, DemoActivity calls the
following registerInBackground() method to register. Note that because the GCM
methods register() and unregister() are blocking, this has to
take place on a background thread. This sample uses AsyncTask
to accomplish this:

/**
* Sends the registration ID to your server over HTTP, so it can use GCM/HTTP
* or CCS to send messages to your app. Not needed for this demo since the
* device sends upstream messages to a server that echoes back the message
* using the 'from' address in the message.
*/
private void sendRegistrationIdToBackend() {
// Your implementation here.
}

After registering, the app calls storeRegistrationId() to store the
registration ID in shared preferences for future use. This is just one way of
persisting a registration ID. You might choose to use a different approach in
your app:

Unregistration

A client app can unregister with GCM using unregister(). This unregisters all the registration tokens
that the client app has with GCM. This can be used to stop the client app from receiving any messages.

Note that it might take a while for the registration ID be completely removed from GCM.
Thus it is possible that messages get a valid message ID as a response, even though
the message will not be delivered to the client app. Eventually, the registration ID
will be removed and the server will get a NotRegistered error, without any further action
being required from the app server (this scenario happens frequently while an app is being developed and tested).

Handle registration errors

An Android app must register with GCM connection servers and get a registration ID
before it can receive messages. A given registration ID is not guaranteed to last indefinitely,
so the first thing your app should always do is check to make sure it has a valid
registration ID (as shown in the code snippets above).

In addition to confirming that it has a valid registration ID, your app should be prepared to handle
the registration error TOO_MANY_REGISTRATIONS. This error indicates that the device
has too many apps registered with GCM. The error only occurs in cases where there are
extreme numbers of apps, so it should not affect the average user. The remedy is to prompt
the user to delete some of the other client apps from the device to make
room for the new one.