Professional Documents
Culture Documents
Checking Validity Server Key
Checking Validity Server Key
com/cloud-messaging/http
See the Server Reference for a list of all the message parameters and which
connection server(s) supports them.
Authentication
To send a message, the application server issues a POST request. For
example:
https://gcm-http.googleapis.com/gcm/send
For example:
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
...
},
The HTTP body content depends on whether you're using JSON or plain text.
See the Server Reference for a list of all the parameters your JSON or plain text
message can contain.
# server_key=YOUR_SERVER_KEY
--header Content-Type:"application/json" \
https://gcm-http.googleapis.com/gcm/send \
-d "{\"registration_ids\":[\"ABC\"]}"
If you receive a 401 HTTP status code, your server key is not valid. Otherwise
you should see something like this:
{"multicast_id":6782339717028231855,"success":0,"failure":1,
"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Request Format
This section shows you how to format requests for both JSON and plain text.
See the Server Reference for a complete list of the fields you can include in a
request.
Send-to-sync
Here is the smallest possible request (a message without any parameters and
just one recipient) using JSON:
{ "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..." }
registration_id=42
{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
You can have messages with both notification and data payloads. See
the Server Reference for supported keys in notification payloads. For more
information about notification message and data message options,
see Payload.
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
{ "collapse_key": "score_update",
"time_to_live": 108,
"data": {
"score": "4x8",
"time": "15:16.2342"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
And here is the same message using plain-text format:
collapse_key=score_update&time_to_live=108&data.score=4x8&data.time
=15:16.2342®istration_id=42
If your organization has a firewall that restricts the traffic to or from the Internet,
you need to configure it to allow connectivity with GCM in order for your GCM
client apps to receive messages. The ports to open are: 5228, 5229, and 5230.
GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM
doesn't provide specific IPs, so you should allow your firewall to accept outgoing
connections to all IP addresses contained in the IP blocks listed in Google's
ASN of 15169.
Response format
There are two possible outcomes when trying to send a message:
The message is processed successfully. The HTTP response has a 200 status,
and the body contains more information about the status of the message.
GCM rejects the request. The HTTP response contains a non-200 status code
(such as 400, 401 or 5xx).
When a JSON request is successful (HTTP status code 200), the JSON object
returned contains the Downstream HTTP message response body. Log the
message ID value for all messages, in case you need to troubleshoot the
message with Google support or through GCM diagnostics.
If the value of failure and canonical_ids is 0, it's not necessary to parse the
remainder of the response. Otherwise, we recommend that you iterate through
the results field and do the following for each object in that list:
Example responses
This section shows a few examples of responses indicating messages that were
processed successfully. See Request Format for the requests these responses
are based on.
{ "multicast_id": 108,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{ "message_id": "1:08" }
]
}
id=1:08
Here are JSON results for 6 recipients (IDs 4, 8, 15, 16, 23, and 42
respectively) with 3 messages successfully processed, 1 canonical registration
token returned, and 3 errors:
{ "multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}
In this example:
Or if just the third message above was sent using plain-text format:
Error=InvalidRegistration
If the fifth message above was also sent using plain-text format:
id=1:2342
registration_id=32
Except as otherwise noted, the content of this page is licensed under the Creative Commons
Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For
details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.