├── example_push.php ├── README.md └── GCMPushMessage.php /example_push.php: -------------------------------------------------------------------------------- 1 | setDevices($devices); 14 | 15 | $response = $an->send($message); 16 | 17 | echo $response; 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Google Cloud Push Messaging PHP Server Class 2 | -------------------------------------------- 3 | 4 | A PHP class to send messages to devices registered through Google Cloud Messaging. 5 | 6 | See: 7 | http://developer.android.com/guide/google/gcm/index.html 8 | 9 | Based on the code available at: 10 | http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging 11 | 12 | Example usage 13 | ----------------------- 14 | ```php 15 | $apiKey = "YOUR GOOGLE API SERVER KEY"; 16 | $devices = array('YOUR REGISTERED DEVICE ID'); 17 | $message = "The message to send"; 18 | 19 | $gcpm = new GCMPushMessage($apiKey); 20 | $gcpm->setDevices($devices); 21 | $response = $gcpm->send($message, array('title' => 'Test title')); 22 | ``` 23 | 24 | How to obtain a Google Server API Key 25 | ----------------------- 26 | - Go to the Google Console https://console.developers.google.com 27 | - Create a new project / open project 28 | 29 | Enable the API 30 | - Click 'ENABLE API' (blue top button) 31 | - Search for 'Google Cloud Messaging', click the link and click 'ENABLE' 32 | 33 | Get a server API key 34 | - Go to https://console.firebase.google.com and select your project your created in the Google console 35 | - Click the cog icon on the left and click 'Project settings' 36 | - Go to the 'CLOUD MESSAGING' tab 37 | - Your server API key will be listed here (for use with this class) 38 | - Your sender ID (for use within your app) will also be listed here 39 | -------------------------------------------------------------------------------- /GCMPushMessage.php: -------------------------------------------------------------------------------- 1 | setDevices($devices); 9 | $response = $an->send($message); 10 | ----------------------- 11 | 12 | $apiKey Your GCM api key 13 | $devices An array or string of registered device tokens 14 | $message The mesasge you want to push out 15 | 16 | @author Matt Grundy 17 | 18 | Adapted from the code available at: 19 | http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging 20 | 21 | */ 22 | class GCMPushMessage { 23 | 24 | // the URL of the GCM API endpoint 25 | private $url = 'https://android.googleapis.com/gcm/send'; 26 | // the server API key - setup on class init 27 | private $serverApiKey = ""; 28 | // array of devices to send to 29 | private $devices = array(); 30 | 31 | /* 32 | Constructor 33 | @param $apiKeyIn the server API key 34 | */ 35 | function GCMPushMessage($apiKeyIn){ 36 | $this->serverApiKey = $apiKeyIn; 37 | } 38 | 39 | /* 40 | Set the devices to send to 41 | @param $deviceIds array of device tokens to send to 42 | */ 43 | function setDevices($deviceIds){ 44 | if(is_array($deviceIds)){ 45 | $this->devices = $deviceIds; 46 | } else { 47 | $this->devices = array($deviceIds); 48 | } 49 | } 50 | 51 | /* 52 | Send the message to the device 53 | @param $message The message to send 54 | @param $data Array of data to accompany the message 55 | */ 56 | function send($message, $data = false){ 57 | 58 | if(!is_array($this->devices) || count($this->devices) == 0){ 59 | throw new GCMPushMessageArgumentException("No devices set"); 60 | } 61 | 62 | if(strlen($this->serverApiKey) < 8){ 63 | throw new GCMPushMessageArgumentException("Server API Key not set"); 64 | } 65 | 66 | $fields = array( 67 | 'registration_ids' => $this->devices, 68 | 'data' => array( "message" => $message ), 69 | ); 70 | 71 | if(is_array($data)){ 72 | foreach ($data as $key => $value) { 73 | $fields['data'][$key] = $value; 74 | } 75 | } 76 | 77 | $headers = array( 78 | 'Authorization: key=' . $this->serverApiKey, 79 | 'Content-Type: application/json' 80 | ); 81 | 82 | // Open connection 83 | $ch = curl_init(); 84 | 85 | // Set the url, number of POST vars, POST data 86 | curl_setopt( $ch, CURLOPT_URL, $this->url ); 87 | 88 | curl_setopt( $ch, CURLOPT_POST, true ); 89 | curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers); 90 | curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); 91 | 92 | curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) ); 93 | 94 | // Avoids problem with https certificate 95 | curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false); 96 | curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false); 97 | 98 | // Execute post 99 | $result = curl_exec($ch); 100 | 101 | // Close connection 102 | curl_close($ch); 103 | 104 | return $result; 105 | } 106 | 107 | } 108 | 109 | class GCMPushMessageArgumentException extends Exception { 110 | public function __construct($message, $code = 0, Exception $previous = null) { 111 | parent::__construct($message, $code, $previous); 112 | } 113 | 114 | public function __toString() { 115 | return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; 116 | } 117 | } 118 | 119 | --------------------------------------------------------------------------------