page.title=Implementing GCM Server @jd:body
The server side of Google Cloud Messaging (GCM) consists of 2 components:
Here are the basic steps you follow to implement your 3rd-party app server:
A full GCM implementation requires both a client implementation and a server implementation. For more information about implementing the client side, see Implementing GCM Client.
Currently GCM provides two connection servers: HTTP and CCS (XMPP). You can use them separately or in tandem. CCS messaging differs from GCM HTTP messaging in the following ways:
Before you can write client Android applications that use the GCM feature, you must have an application server that meets the following criteria:
Here is the general sequence of events that occurs when a 3rd-party application server sends a message:
The following sections describe the basic requirements for sending messages.
Required. When your app server sends a message in GCM, it must specify a target.
For HTTP you must specify the target as one of:
registration_ids
: For sending to 1 or more devices (up to 1000).
When you send a message to multiple registration IDs, that is called a multicast message.notification_key
: For sending to multiple devices owned by a single user.For CCS (XMPP):
Optional. If you are including a payload in the message, you use the data
parameter to include the payload. This applies for both HTTP and CCS.
The following table lists the parameters that a 3rd-party app server might include in the JSON messages it sends to a connection server. See the "Where Supported" column for information about which connection servers support that particular parameter.
Table 1. Message Parameters JSON (CCS and HTTP).
Parameter | Description | Where Supported | to |
In CCS, this parameter is used in place of registration_ids to
specify the recipient of a message. Its value must be a registration ID.
The value is a string. Required. |
CCS |
---|---|---|
message_id |
In CCS, this parameter uniquely identifies a message in an XMPP connection. The value is a string that uniquely identifies the associated message. Required. | CCS |
message_type |
In CCS, this parameter indicates a special status message, typically sent by the system. However, your app server also uses this parameter to send an 'ack' or 'nack' message back to the CCS connection server. For more discussion of this topic, see Cloud Connection Server. The value is a string. Optional. | CCS |
registration_ids |
This parameter specifies a string array containing the list of devices (registration IDs) receiving the message. It must contain at least 1 and at most 1000 registration IDs. To send a multicast message, you must use JSON. For sending a single message to a single device, you could use a JSON object with just 1 registration id, or plain text (see below). A request must include a recipient—this can be either a registration ID, an array of registration IDs, or a {@code notification_key}. Required. | HTTP |
notification_key |
This parameter specifies a string that maps a single user to multiple registration IDs associated with that user. This allows a 3rd-party server to send a single message to multiple app instances (typically on multiple devices) owned by a single user. A 3rd-party server can use {@code notification_key} as the target for a message instead of an individual registration ID (or array of registration IDs). The maximum number of members allowed for a {@code notification_key} is 20. For more discussion of this topic, see User Notifications. Optional. | HTTP. This feature is supported in CCS, but you use it by specifying a notification key in the "to" field. |
collapse_key |
This parameter specifies an arbitrary string (such as
"Updates Available") that is used
to collapse a group of like messages
when the device is offline, so that only the last message gets sent to the
client. This is intended to avoid sending too many messages to the phone when it
comes back online. Note that since there is no guarantee of the order in which
messages get sent, the "last" message may not actually be the last
message sent by the application server. Messages with collapse keys are also called
send-to-sync messages.
Note: GCM allows a maximum of 4 different collapse keys to be used by the GCM server at any given time. In other words, the GCM server can simultaneously store 4 different send-to-sync messages per device, each with a different collapse key. If you exceed this number GCM will only keep 4 collapse keys, with no guarantees about which ones they will be. See Advanced Topics for more discussion of this topic. Optional. |
CCS, HTTP |
data |
This parameter specifies a JSON object whose fields represents the
key-value pairs of the message's
payload data. If present, the payload data will be
included in the Intent as application data, with the key being the extra's name.
For instance, "data":{"score":"3x1"} would result in an intent extra
named score whose value is the string 3x1 .
There is no limit on the number of key/value pairs, though there is a limit on
the total size of the message (4kb). The values could be any JSON object, but we
recommend using strings, since the values will be converted to strings in the GCM
server anyway. If you want to include objects or other non-string data types
(such as integers or booleans), you have to do the conversion to string yourself.
Also note that the key cannot be a reserved word (from or any word
starting with google. ). Using words defined in this table as field
names (such as collapse_key ) could yield unpredictable outcomes and
is not recommended. Optional. |
CCS, HTTP |
delay_while_idle |
This parameter indicates that the message should not be sent immediately
if the device is idle. The server will wait for the device to become active, and
then only the last message for each collapse_key value will be
sent. The default value is false , and must be a JSON boolean. Optional. |
CCS, HTTP |
time_to_live |
This parameter specifies how long (in seconds) the message should be kept on GCM storage if the device is offline. Optional (default time-to-live is 4 weeks, and must be set as a JSON number). | CCS, HTTP |
restricted_package_name |
This parameter specifies a string containing the package name of your application. When set, messages are only sent to registration IDs that match the package name. Optional. | HTTP |
dry_run |
This parameter allows developers to test a request without actually
sending a message. Optional. The default value is false , and must
be a JSON boolean.
|
HTTP |
delivery_receipt_requested |
This parameter lets you request confirmation of message delivery. When
this parameter is set to true , CCS sends a
delivery receipt when a device confirms that it received a message sent by CCS.
The default value is false , and must be a JSON boolean. Optional.This parameter relates to delivery receipts. |
CCS |
message_status |
This parameter specifies the status of the receipt message.
The parameter appears inside the
"data" field of a
delivery receipt message. Currently the only possible value
is MESSAGE_SENT_TO_DEVICE , which indicates that a device acknowledges
receiving a message sent by CCS.This parameter relates to delivery receipts. |
CCS |
original_message_id |
The value of this parameter is the ID of the original message that the server sent to
the device. This parameter appears inside the "data" field of a
delivery receipt message. This parameter relates to delivery receipts. |
CCS |
device_registration_id |
For the purpose of tracking the delivery receipt, this parameter lists
the registration ID of the device to which a given message was sent. This parameter
appears inside the "data" field of a
delivery receipt message. This parameter relates to delivery receipts. |
CCS |
If you want to test your request (either JSON or plain text) without delivering
the message to the devices, you can set an optional HTTP or JSON parameter called
dry_run
with the value true
. The result will be almost
identical to running the request without this parameter, except that the message
will not be delivered to the devices. Consequently, the response will contain fake
IDs for the message and multicast parameters.
If you are using plain text instead of JSON, the message parameters must be set as HTTP parameters sent in the body, and their syntax is slightly different, as described in the following table:
Table 2. Message Parameters Plain Text (HTTP only).
Parameter | Description |
---|---|
registration_id |
This parameter specifies the registration ID of the single device receiving the message. Required. |
collapse_key |
Same as JSON (see previous table). Optional. |
data.<key> |
This parameter specifies payload data, expressed as parameters
prefixed with data. and
suffixed as the key. For instance, a parameter of data.score=3x1 would
result in an intent extra named score whose value is the string
3x1 . There is no limit on the number of key/value parameters, though
there is a limit on the total size of the message. Also note that the key cannot
be a reserved word (from or any word starting with
google. ). Using words defined in this table as field
names (such as collapse_key ) could yield unpredictable outcomes and
is not recommended. Optional. |
delay_while_idle |
This parameter specifies whether messages should be delivered when the device
is asleep. A value of 1 or true indicates
true , and anything else indicates false . Optional. The default
value is false . |
time_to_live |
Same as JSON (see previous table). Optional. |
restricted_package_name |
Same as JSON (see previous table). Optional. |
dry_run |
Same as JSON (see previous table). Optional. |
This is the sequence of events that occurs when an Android application installed on a mobile device receives a message:
com.google.android.c2dm.intent.RECEIVE
Intent as a set of
extras.com.google.android.c2dm.intent.RECEIVE
Intent
by key and processes the data.See the documentation for each connection server for more detail on how it handles responses.