As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as May 29, 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.

Google Cloud Messaging: Overview

Google Cloud Messaging (GCM) is a free service that enables developers
to send messages between servers and client apps. This includes downstream messages
from servers to client apps, and
upstream messages from client apps to servers.

For example, a lightweight downstream message could inform a client app
that there is new data to be fetched from the server, as in the case of a "new email"
notification. For use cases such as instant messaging, a GCM message can transfer
up to 4kb of payload to the client app. The GCM
service handles all aspects of queueing of messages and delivery to and from
the target client app.

Architectural Overview

A GCM implementation includes a Google
connection server, an app server in your environment that interacts with the connection
server via HTTP or XMPP protocol, and a client app.

Figure 1. GCM Architecture.

Here's how these components interact:

Google GCM Connection Servers accept downstream messages from
your app server and send them to a client app. The XMPP connection
server can also
accept messages sent upstream from the client app and forward them to your app server.
For more information, see About GCM Connection Server.

On your App Server, you
implement the HTTP
and/or XMPP protocol to communicate with the GCM connection server(s). App servers send
downstream messages to a GCM connection server; the connection server enqueues and stores the
message, and then sends it to the client app. If you implement XMPP, your app server can receive
messages sent from the client app.

The Client App is a GCM-enabled client app.
To receive and send GCM messages, this app must register with GCM and get a unique
identifier called a registration token.
For more information on how to implement the client app, see
the documentation for your platform.

Key Concepts

This table summarizes the key terms and concepts involved in GCM. It is
divided into these categories:

Components — The entities that play a primary role in
GCM.

Credentials — The IDs and tokens that are used in
GCM to ensure that all parties have been authenticated, and
that the message is going to the correct place.

Table 1. GCM components and credentials.

Components

GCM Connection Servers

Google servers involved in sending messages between the
app server and the client app.

Client App

A GCM-enabled client app that communicates with your app server.

App Server

An app server that you write as part of implementing
GCM. The app server sends data to a client app via
the GCM connection server. If your app server implements the XMPP protocol,
it can also receive messages sent upstream from client apps.

Credentials

Sender ID

A unique numerical value created when you configure your API project. The sender
ID is used in the registration process to identify an
app server that is permitted to send messages to the client app.

Server key

A key saved on the app server that gives the app server authorized
access to Google services. In HTTP, the server key is included in the header
of POST requests that send messages. In XMPP, the server key is used in the
SASL PLAIN authentication request as a password to authenticate the connection.
Do not include the server key anywhere in your client code.Starting from September 2016, you can create new server keys only in the
Firebase Console using the
Cloud Messaging
tab of the Settings panel.
Existing projects that need to create a new server key can be imported in the
Firebase console
without affecting their existing configuration.

Application ID

The client app that is registering to receive messages. How this is implemented
is platform-dependent:

Android: use the package name from the app manifest.

iOS: use the app's bundle identifier.

Chrome: use the Chrome extension name.

Registration Token

An ID issued by the GCM connection servers to the client app that allows
it to receive messages. Note that registration tokens must be kept secret.

Lifecycle Flow

Register to enable GCM. An instance of a client app registers to receive messages.
For more discussion, see Registering Client Apps.

The GCM connection server enqueues and stores the message if the device is offline.

When the device is online, the GCM connection server sends the message to the device.

On the device, the client app receives the message according to the platform-specific implementation.
See your platform-specific documentation for details.

Receive a message. A client app
receives a message from a GCM connection server. See your platform-specific documentation for details
on how a client app in that environment processes the messages it receives.

Send and receive upstream messages. This feature is only available if
you're using the XMPP connection server.

Send a message. A client app sends messages to the app server:

On the device, the client app sends messages to the XMPP connection server. See your platform-specific
documentation for details on how a client app can send a message via XMPP.

The XMPP connection server enqueues and stores the message if the server is disconnected.

When the app server is re-connected, the XMPP connection server sends the message to the app server.

Receive a message. An app server receives a message from the XMPP connection server and then does the following:

Parses the message header to verify client app sender information.

Sends "ack" to the XMPP connection server to acknowledge receiving the message.