• Advertisement

  • Advertisements

GCM server Side php

Here is the server side programming for GCM (GOOGLE CLOUD MESSAGING) I have not tried this yet but I found it here: https://github.com/mattg888/GCM-PHP-Server-Push-Message

$response = $an->send($message);

$apiKey Your GCM api key
$devices An array or string of registered device tokens
$message The mesasge you want to push out

@author Matt Grundy

Adapted from the code available at:

class GCMPushMessage {

var $url = ‘https://android.googleapis.com/gcm/send’;
var $serverApiKey = “”;
var $devices = array();

function GCMPushMessage($apiKeyIn){
$this->serverApiKey = $apiKeyIn;

function setDevices($deviceIds){

$this->devices = $deviceIds;
} else {
$this->devices = array($deviceIds);


function send($message){

if(!is_array($this->devices) || count($this->devices) == 0){
$this->error(“No devices set”);

if(strlen($this->serverApiKey) error(“Server API Key not set”);

$fields = array(
‘registration_ids’ => $this->devices,
‘data’ => array( “message” => $message ),

$headers = array(
‘Authorization: key=’ . $this->serverApiKey,
‘Content-Type: application/json’

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $this->url );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);

// Close connection

return $result;

function error($msg){
echo “Android send notification failed with error:”;
echo “\t” . $msg;



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: