Using PHP to send a message to a Google Cloud Messaging registered device is quite simple. In this example we'll be using curl to create a message request to send to the Google GCM server.
The code shown in this example is just going to demonstrate how to send a message to a registered device ID.
I'll leave the particulars of communicating the device registration id's up to you, however the process is fairly simple:
- Register with the GCM service on your device, retrieving a registration ID and store in the application (This can be done through our native extension).
- Send the registration ID to your server and store it on your server as a list of connected devices
- When you wish to send a notification, use the appropriate registration ID in the data of your message
Firstly you'll need to sign up to the Google Cloud Messaging service. This process is outlined in the "Getting Started" section on the Android developer site: http://developer.android.com/guide/google/gcm/gs.html.
You need to follow these instructions to get your API keys. If you're using our native extension in AIR then anything further can be ignored (i.e. anything from "Install the Helper Libraries"), though you will need it when developing your own native application.
Once you've got your api key and your device id you're ready to send a message. The PHP function below implements a simple "send notification" function using a JSON formatted message package.
- * The following function will send a GCM notification using curl.
- * @param $apiKey [string] The Browser API key string for your GCM account
- * @param $registrationIdsArray [array] An array of registration ids to send this notification to
- * @param $messageData [array] An named array of data to send as the notification payload
- function sendNotification( $apiKey, $registrationIdsArray, $messageData )
- $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
- $data = array(
- 'data' => $messageData,
- 'registration_ids' => $registrationIdsArray
- $ch = curl_init();
- curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
- curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
- curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
- curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
- curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
- $response = curl_exec($ch);
- return $response;
To use this function you'll need to assemble the message data and an array of registration ids, as demonstrated below. The message data we construct here is an example of usage with our extension to automate the display of a notification when the GCM arrives on the device.
- // Message to send
- $message = "the test message";
- $tickerText = "ticker text message";
- $contentTitle = "content title";
- $contentText = "content body";
- $registrationId = 'DEVICE_ID';
- $apiKey = "YOUR_BROWSER_API_KEY";
- $response = sendNotification(
- array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, "contentText" => $contentText) );
- echo $response;
The echoed response will show the JSON GCM message package sent to the server, and as long as you have the correct device ID and a registered device, your notification will appear on your device. Happy messaging!