From 5e5b6c79626cd6b1b2dcb827216113a57d40b163 Mon Sep 17 00:00:00 2001 From: Franklin Waller Date: Thu, 28 Aug 2014 17:51:54 +0200 Subject: [PATCH 1/2] Use __construct instead of same classname. --- GCMPushMessage.php | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/GCMPushMessage.php b/GCMPushMessage.php index c24c404..0073309 100644 --- a/GCMPushMessage.php +++ b/GCMPushMessage.php @@ -19,17 +19,21 @@ http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging */ + +namespace Google\GCM; + 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){ + */ + + public function __construct($apiKeyIn){ $this->serverApiKey = $apiKeyIn; } @@ -38,13 +42,13 @@ function GCMPushMessage($apiKeyIn){ @param $deviceIds array of device tokens to send to */ function setDevices($deviceIds){ - + if(is_array($deviceIds)){ $this->devices = $deviceIds; } else { $this->devices = array($deviceIds); } - + } /* @@ -53,20 +57,20 @@ function setDevices($deviceIds){ @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; @@ -80,25 +84,25 @@ function send($message, $data = false){ // 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 curl_close($ch); - + return $result; } - + function error($msg){ echo "Android send notification failed with error:"; echo "\t" . $msg; From abf9543f4f16b0ee0875c57d4d557faa4761060f Mon Sep 17 00:00:00 2001 From: Franklin Waller Date: Thu, 28 Aug 2014 17:53:18 +0200 Subject: [PATCH 2/2] Deleted accidantal namespace --- GCMPushMessage.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/GCMPushMessage.php b/GCMPushMessage.php index 0073309..0e0cc49 100644 --- a/GCMPushMessage.php +++ b/GCMPushMessage.php @@ -20,8 +20,6 @@ */ -namespace Google\GCM; - class GCMPushMessage { var $url = 'https://android.googleapis.com/gcm/send';