page.title=Overview @jd:body
Google Cloud Messaging (GCM) is a free service that enables developers to send downstream messages (from servers to GCM-enabled client apps), and upstream messages (from the GCM-enabled client apps to servers). This could be a lightweight message telling the client app that there is new data to be fetched from the server (for instance, a "new email" notification informing the app that it is out of sync with the back end), or it could be a message containing up to 4kb of payload data (so apps like instant messaging can consume the message directly). The GCM service handles all aspects of queueing of messages and delivery to and from the target client app.
This table summarizes the key terms and concepts involved in GCM. It is divided into these categories:
Components | |
---|---|
GCM Connection Servers | The Google-provided servers involved in sending messages between the 3rd-party app server and the client app. |
Client App | A GCM-enabled client app that communicates with a 3rd-party app server. |
3rd-party App Server | An app server that you write as part of implementing GCM. The 3rd-party app server sends data to a client app via the GCM connection server. |
Credentials | |
Sender ID | A project number you acquire from the API console, as described in Getting Started. The sender ID is used in the registration process to identify a 3rd-party app server that is permitted to send messages to the client app. |
Sender Auth Token | An API key that is saved on the 3rd-party app server that gives the app server authorized access to Google services. The API key is included in the header of POST requests. |
Application ID | The client app that is registering to receive messages. How this is implemented is platform-dependent. For example, an Android app is identified by the package name from the manifest. This ensures that the messages are targeted to the correct Android app. |
Registration ID | An ID issued by the GCM servers to the client app that allows it to receive messages. Note that registration IDs must be kept secret. |
A GCM implementation includes a Google-provided connection server, a 3rd-party app server that interacts with the connection server, and a GCM-enabled client app. For example, this diagram shows GCM communicating with a client app on an Android device:
This is how these components interact:
Regardless of the platform you're developing on, the first step a client app must do is register with GCM. This section covers some of the general best practices for registration and unregistration. See your platform-specific docs for details on writing a GCM-enabled client app on that platform.
Whenever the app registers as described in Implementing GCM Client, it should save the registration ID for future use, pass it to the 3rd-party server to complete the registration, and keep track of whether the server completed the registration. If the server fails to complete the registration, the client app should retry passing the registration ID to 3rd-party app server to complete the registration. If this continues to fail, the client app should unregister from GCM.
There are also two other scenarios that require special care:
If a bug in the app triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.
GCM provides a facility called "canonical registration IDs" to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your app. This is the ID that the server should use when sending messages to the device.
If later on you try to send a message using a different registration ID, GCM
will process the request as usual, but it will include the canonical registration
ID in the registration_id
field of the response. Make sure to replace
the registration ID stored in your server with this canonical ID, as eventually
the ID you're using will stop working.
When registration or unregistration fails, the app should retry the failed operation.
In the simplest case, if your app attempts to register and GCM is not a fundamental part of the app, the app could simply ignore the error and try to register again the next time it starts. Otherwise, it should retry the previous operation using exponential back-off. In exponential back-off, each time there is a failure, it should wait twice the previous amount of time before trying again.
This section explains when you should unregister in GCM and what happens when you do.
You should only need to unregister in rare cases, such as if you want an app to stop receiving messages, or if you suspect that the registration ID has been compromised. In general, once an app has a registration ID, you shouldn't need to change it.
In particular, you should never unregister your app as a mechanism for logout or for switching between users, for the following reasons:
To make sure that messages go to the intended user:
An app can be automatically unregistered after it is uninstalled. However, this process does not happen right away. What happens in this scenario is as follows:
NotRegistered
error message to the 3rd-party app server.Note that it might take a while for the registration ID be completely removed
from GCM. Thus it is possible that messages sent during step 7 above gets a valid
message ID as 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 3rd-party server (this scenario happens frequently while an app is
being developed and tested).