How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Google Cloud Messaging (GCM) is a free service from Google that helps Android developers to send data from their servers to Android applications on Android devices. GCM is the next generation of Android Cloud to Device Messaging (C2DM) which is currently deprecated but still in service without accepting new users and granting new quotas.

GCM architecture consists of three main components, Android application on mobile device, 3rd-party application server and GCM servers. To send message to Android applications on android devices, 3rd-party application server must send the message to GCM servers and let the GCM servers process and send the message to mobile devices. To learn more on how to setup the project and client application, please refer to GCM documentation that well documented and easy to undersand. GCM accepts two data format, JSON and plain text, but it is recommended to use JSON type for flexibility and easy of use.

To send a message, the application server must issue a POST request to the following url:

The request is made up of two parts:

HTTP header, that must contain the following headers:

  • Authorization:key=your_api_key
  • Content-Type:application/json for JSON data type

HTTP body, which is for JSON data type must contain a string representing  a JSON object with the following fields:

  • registration_ids, is a string array of registration IDs. Registration IDs  are sent from android applications on mobile devices and saved in application server database.
  • data, is the payload data in array of key-value pair.
  • collapse_key (optional), is an arbitrary string 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.
  • time_to_live (optional), time in seconds where the message should be kept on GCM storage if the device is offline
  • delay_while_iddle (optional), a flag to indicate the message should not be sent immediately if the device is idle (true or false)

Example request:

Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA{"registration_ids":["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],"data":{"score" : "1-0", "scorer" : "Ronaldo", "time: "44"},}

To send the above message using PHP, define your api key (line 6), the string array of registration IDs (line 9), the payload data in array of key-value pair (line 12), the HTTP headers which contains api key  (line 18) and the HTTP body content in JSON format, use json_encode function to transform the array into JSON object (line 29). The HTTP request type is POST and we use CURL to send the message to GCM server.

Every request will return a response, which might be success (the message is processed successfully) or rejected by GCM server. More detail on how to process the response can be read on this documention.

PHP code

//request url
$url    = '';

//your api key
$apiKey = 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA';

//registration ids
$registrationIDs = array('APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...');

//payload data
$data   = array('score' => '1-0', 'scorer' => 'Ronaldo', 'time' => '44');

$fields = array('registration_ids' => $registrationIDs,
                'data' => $data);

//http header
$headers = array('Authorization: key=' . $apiKey,
                 'Content-Type: application/json');

//curl connection
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

$result = curl_exec($ch);


echo $result;
facebooktwittergoogle_plusredditpinterestlinkedinmailby feather


  1. himanshu says

    Hello, I am using wamp local server to run this but i got this error on php output : {“multicast_id”:5754693764770083005,”success”:0,”failure”:1,”canonical_ids”:0,”results”:[{"error":"MismatchSenderId"}]}

    Please help me to acheive this…

  2. says

    I’m really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it’s rare to see a nice blog like this one today.

  3. cmiklos says


    I only get “null” messages in the device using your code or others.. can´t find the problem.

    Can you help me?

  4. macio.Jun says

    Hi PO,

    Can I use
    header(‘Content-Type: application/json’);
    $outputData = array(“name” => “John”);
    echo $outputData;

    // But what should I do next to send $outputData to
    // And how can I get response from gcm server?
    // Do I have to use curl to send data to gcm server?

    Thank you

  5. David Cobas says

    Hi, im using you PHP code to send a gcm message but i get this response…
    Why i get a invalidRegistration error? pls i need help

Leave a Reply

Your email address will not be published. Required fields are marked *