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:
https://android.googleapis.com/gcm/send
The request is made up of two parts:
HTTP header, that must contain the following headers:
HTTP body, which is for JSON data type must contain a string representing a JSON object with the following fields:
Example request:
Content-Type:application/json 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
<?php
//request url
$url = 'https://android.googleapis.com/gcm/send';
//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);
curl_close($ch);
echo $result;
?>
Related post:
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…
Actually we are using XAMPP and WAMP server for PHP.but using GCM server that i have heard from your site that we will try on that.
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.
Very Helpful
BUt i have a problem with blank notifications
How can we solve it