From 978b8b35bacfa8c4f05860e4ea96672766865eae Mon Sep 17 00:00:00 2001 From: Artem Yankovskiy Date: Thu, 12 Mar 2015 16:58:58 +1000 Subject: [PATCH 1/3] Changed formatting and documenation --- .gitignore | 3 + GCMPushMessage.php | 219 +++++++++++++++++++++++---------------------- 2 files changed, 115 insertions(+), 107 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b17a33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.settings/ +.project +.buildpath diff --git a/GCMPushMessage.php b/GCMPushMessage.php index 38e6bba..ec077e6 100644 --- a/GCMPushMessage.php +++ b/GCMPushMessage.php @@ -1,111 +1,116 @@ setDevices($devices); - $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: - http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging - -*/ +/** + * Class to send push notifications using Google Cloud Messaging for Android + * + * Example usage + * ----------------------- + * $an = new GCMPushMessage($apiKey); + * $an->setDevices($devices); + * $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: + * http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging + * + * Modify: Artem Yankovskiy (artemyankovskiy@gmail.com) + * + **/ class GCMPushMessage { - var $url = 'https://android.googleapis.com/gcm/send'; - var $serverApiKey = ""; - var $devices = array(); - - /* - Constructor - @param $apiKeyIn the server API key - */ - function GCMPushMessage($apiKeyIn){ - $this->serverApiKey = $apiKeyIn; - } - - /* - Set the devices to send to - @param $deviceIds array of device tokens to send to - */ - function setDevices($deviceIds){ - - if(is_array($deviceIds)){ - $this->devices = $deviceIds; - } else { - $this->devices = array($deviceIds); - } - - } - - /* - Send the message to the device - @param $message The message to send - @param $data Array of data to accompany the message - */ - function send($message, $data = false){ - - if(!is_array($this->devices) || count($this->devices) == 0){ - $this->error("No devices set"); - } - - if(strlen($this->serverApiKey) < 8){ - $this->error("Server API Key not set"); - } - - $fields = array( - 'registration_ids' => $this->devices, - 'data' => array( "message" => $message ), - ); - - if(is_array($data)){ - foreach ($data as $key => $value) { - $fields['data'][$key] = $value; - } - } - - $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 ) ); - - // Avoids problem with https certificate - curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false); - - // Execute post - $result = curl_exec($ch); - - // Close connection - curl_close($ch); - - return $result; - } - - function error($msg){ - echo "Android send notification failed with error:"; - echo "\t" . $msg; - exit(1); - } + private $url = 'https://android.googleapis.com/gcm/send'; + private $serverApiKey = ""; + private $devices = array(); + + /** + * Constructor + * @param $apiKeyIn the server API key + */ + function __construct($apiKeyIn) { + $this->serverApiKey = $apiKeyIn; + } + + /** + * Set the devices to send to + * @param mixed $deviceIds array of devices tokens for send to + */ + public function setDevices($deviceIds) { + + if (is_array($deviceIds)) { + $this->devices = $deviceIds; + } else { + $this->devices = array($deviceIds); + } + } + + /** + * Send data to the device + * @param string $message data for send + * @param mixed $data (optional) array of data to accompany the message + */ + public function send($message, $data = false) { + + if (!is_array($this->devices) || count($this->devices) == 0) { + $this->error("No devices set"); + } + + if (strlen($this->serverApiKey) < 8) { + $this->error("Server API Key not set"); + } + + $fields = array( + 'registration_ids' => $this->devices, + 'data' => array( "message" => $message ) + ); + + if (is_array($data)) { + foreach ($data as $key => $value) { + $fields['data'][$key] = $value; + } + } + + $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 ) ); + + // Avoids problem with https certificate + curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false); + + // Execute post + $result = curl_exec($ch); + + // Close connection + curl_close($ch); + + return $result; + } + + /** + * Prints error message to screen and exit from app + * @param string $msg error message + */ + private function error($msg) { + echo "Android send notification failed with error:"; + echo "\t" . $msg; + exit(1); + } } From af8766dc0108d9e2f1aaf75aea53c3c07d700534 Mon Sep 17 00:00:00 2001 From: Artem Yankovskiy Date: Thu, 19 Mar 2015 15:13:25 +1000 Subject: [PATCH 2/3] Function send return body and status --- GCMPushMessage.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/GCMPushMessage.php b/GCMPushMessage.php index ec077e6..4dc6052 100644 --- a/GCMPushMessage.php +++ b/GCMPushMessage.php @@ -52,6 +52,7 @@ public function setDevices($deviceIds) { * Send data to the device * @param string $message data for send * @param mixed $data (optional) array of data to accompany the message + * @return assoc array ("body", "http_code") */ public function send($message, $data = false) { @@ -96,12 +97,13 @@ public function send($message, $data = false) { curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false); // Execute post - $result = curl_exec($ch); - + $httpBody = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + // Close connection curl_close($ch); - return $result; + return array("http_body" => $httpBody, "http_code" => $httpCode); } /** From 430602292dfcabeb4374d108a579afa192953eaa Mon Sep 17 00:00:00 2001 From: Artem Yankovskiy Date: Mon, 23 Mar 2015 09:07:05 +1000 Subject: [PATCH 3/3] Added collapse key support --- GCMPushMessage.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/GCMPushMessage.php b/GCMPushMessage.php index 4dc6052..380269c 100644 --- a/GCMPushMessage.php +++ b/GCMPushMessage.php @@ -52,9 +52,10 @@ public function setDevices($deviceIds) { * Send data to the device * @param string $message data for send * @param mixed $data (optional) array of data to accompany the message + * @param string $collapseKey (optional) collapse key for message * @return assoc array ("body", "http_code") */ - public function send($message, $data = false) { + public function send($message, $data = false, $collapseKey = null) { if (!is_array($this->devices) || count($this->devices) == 0) { $this->error("No devices set"); @@ -68,6 +69,10 @@ public function send($message, $data = false) { 'registration_ids' => $this->devices, 'data' => array( "message" => $message ) ); + + if (isset($collapseKey)) { + $fields["collapse_key"] = $collapseKey; + } if (is_array($data)) { foreach ($data as $key => $value) {