diff --git a/config/gcm.php b/config/gcm.php index 09949b8..3d292a3 100644 --- a/config/gcm.php +++ b/config/gcm.php @@ -1,11 +1,11 @@ 'Maybe missed API key', + 'MismatchSenderId' => 'Make sure you\'re using one of those when trying to send messages to the device. If you switch to a different sender, the existing registration IDs won\'t work.', + 'MissingRegistration' => 'Check that the request contains a registration ID', + 'InvalidRegistration' => 'Check the formatting of the registration ID that you pass to the server. Make sure it matches the registration ID the phone receives in the google', + 'NotRegistered' => 'Not registered', + 'MessageTooBig' => 'The total size of the payload data that is included in a message can\'t exceed 4096 bytes' + ); + + /** + * Constructor + */ + public function __construct() + { + $ci =& get_instance(); + $ci->load->config('gcm', true); + $this->api_key = $ci->config->item('gcm_api_key', 'gcm'); + $this->api_send_address = $ci->config->item('gcm_api_send_address', 'gcm'); + if ( ! $this->api_key) + { + show_error('GCM: Needed API Key'); + } + if ( ! $this->api_send_address) + { + show_error('GCM: Needed API Send Address'); + } + } + + /** + * Sets additional data which will be send with main apn message + * + * @param $data + * @return + */ + public function set_ttl($ttl = '') + { + if ( ! $ttl) + { + unset($this->payload['time_to_live']); + } + else + { + $this->payload['time_to_live'] = $ttl; + } + } + + /** + * Setting GCM message + * + * @param $message + */ + public function set_message($message = '') + { + $this->message = $message; + $this->payload['data']['message'] = $message; + } + + /** + * Setting data to message + * + * @param $data + */ + public function set_data($data = array()) + { + $this->payload['data'] = $data; + if ($this->message) + { + $this->payload['data']['message'] = $this->message; + } + } + + /** + * Setting group of messages + * + * @param $group + */ + public function set_group($group = '') + { + if ( ! $group) + { + unset($this->payload['collapse_key']); + } + else + { + $this->payload['collapse_key'] = $group; + } + } + + /** + * Adding one recipient + * + * @param $group + */ + public function add_recepient($registration_id) + { + $this->payload['registration_ids'][] = $registration_id; + } + + /** + * Setting all recipients + * + * @param $group + */ + public function set_recepients($registration_ids) + { + $this->payload['registration_ids'] = $registration_ids; + } + + /** + * Clearing group of messages + */ + public function clear_recepients() + { + $this->payload['registration_ids'] = array(); + } + + /** + * Sending messages to Google Cloud Messaging + * + * @param $group + */ + public function send() + { + $this->payload['registration_ids'] = array_unique($this->payload['registration_ids']); + sort($this->payload['registration_ids']); + if (isset($this->payload['time_to_live']) && ! isset($this->payload['collapse_key'])) + { + $this->payload['collapse_key'] = 'GCM Notifications'; + } + $data = json_encode($this->payload); + return $this->request($data); + } + + protected function request($data) + { + $headers[] = 'Content-Type: application/json'; + $headers[] = 'Authorization: key='.$this->api_key; + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $this->api_send_address); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_HEADER, true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + $this->response_data = curl_exec($curl); + $this->response_info = curl_getinfo($curl); + curl_close($curl); + return $this->parseResponse(); + } + + protected function parse_response() + { + if ($this->response_info['http_code'] == 200) + { + $response = explode("\n", $this->response_data); + $response_body = json_decode($response[count($response) - 1]); + if ($response_body->success && ! $response_body->failure) + { + $message = 'All messages were sent successfully'; + $error = FALSE; + } + elseif ($response_body->success && $response_body->failure) + { + $message = $response_body->success.' of '.($response_body->success+$response_body->failure).' messages were sent successfully'; + $error = TRUE; + } + elseif ( ! $response_body->success && $response_body->failure) + { + $message = 'No messages cannot be sent. '.$response_body->results[0]->error; + $error = TRUE; + } + $this->status = array( + 'error' => $error, + 'message' => $message + ); + $this->messages_statuses = array(); + foreach($response_body->results as $key => $result) + { + if (isset($result->error) && $result->error) + { + $this->messages_statuses[$key] = array( + 'error' => TRUE; + 'regid' => $this->payload['registration_ids'][$key], + 'message' => $this->errorStatuses[$result->error], + 'message_id' => '' + ); + } + else + { + $this->messages_statuses[$key] = array( + 'error' => FALSE; + 'regid' => $this->payload['registration_ids'][$key], + 'message' => 'Message was sent successfully', + 'message_id' => $result->message_id + ); + } + } + return ! $error; + } + elseif ($this->response_info['http_code'] == 400) + { + $this->status = array( + 'error' => TRUE; + 'message' => 'Request could not be parsed as JSON' + ); + return FALSE; + } + elseif ($this->response_info['http_code'] == 401) + { + $this->status = array( + 'error' => TRUE; + 'message' => 'There was an error authenticating the sender account' + ); + return FALSE; + } + elseif ($this->response_info['http_code'] == 500) + { + $this->status = array( + 'error' => TRUE; + 'message' => 'There was an internal error in the GCM server while trying to process the request' + ); + return FALSE; + } + elseif ($this->response_info['http_code'] == 503) + { + $this->status = array( + 'error' => TRUE; + 'message' => 'Server is temporarily unavailable' + ); + return FALSE; + } + else + { + $this->status = array( + 'error' => TRUE; + 'message' => 'Status undefined' + ); + return FALSE; + } + } +} diff --git a/libraries/gcm.php b/libraries/gcm.php deleted file mode 100644 index 32ac615..0000000 --- a/libraries/gcm.php +++ /dev/null @@ -1,302 +0,0 @@ - 'Maybe missed API key', - 'MismatchSenderId' => 'Make sure you\'re using one of those when trying to send messages to the device. If you switch to a different sender, the existing registration IDs won\'t work.', - 'MissingRegistration' => 'Check that the request contains a registration ID', - 'InvalidRegistration' => 'Check the formatting of the registration ID that you pass to the server. Make sure it matches the registration ID the phone receives in the google', - 'NotRegistered' => 'Not registered', - 'MessageTooBig' => 'The total size of the payload data that is included in a message can\'t exceed 4096 bytes' - ); - - /** - * Constructor - */ - public function __construct() { - - $ci =& get_instance(); - $ci->load->config('gcm',true); - - $this->apiKey = $ci->config->item('gcm_api_key','gcm'); - $this->apiSendAddress = $ci->config->item('gcm_api_send_address','gcm'); - - if (!$this->apiKey) { - show_error('GCM: Needed API Key'); - } - - if (!$this->apiSendAddress) { - show_error('GCM: Needed API Send Address'); - } - } - - - /** - * Sets additional data which will be send with main apn message - * - * @param $data - * @return - */ - public function setTtl($ttl = '') - { - if (!$ttl) - unset($this->payload['time_to_live']); - else - $this->payload['time_to_live'] = $ttl; - } - - - /** - * Setting GCM message - * - * @param $message - */ - public function setMessage($message = '') { - - $this->message = $message; - $this->payload['data']['message'] = $message; - - } - - - /** - * Setting data to message - * - * @param $data - */ - public function setData($data = array()) { - - $this->payload['data'] = $data; - - if ($this->message) - $this->payload['data']['message'] = $this->message; - - } - - - /** - * Setting group of messages - * - * @param $group - */ - public function setGroup($group = '') { - - if (!$group) - unset($this->payload['collapse_key']); - else - $this->payload['collapse_key'] = $group; - } - - - /** - * Adding one recepient - * - * @param $group - */ - public function addRecepient($registrationId) { - - $this->payload['registration_ids'][] = $registrationId; - } - - - /** - * Setting all recepients - * - * @param $group - */ - public function setRecepients($registrationIds) { - - $this->payload['registration_ids'] = $registrationIds; - } - - - /** - * Clearing group of messages - */ - public function clearRecepients() { - - $this->payload['registration_ids'] = array(); - } - - - /** - * Senging messages to Google Cloud Messaging - * - * @param $group - */ - public function send() - { - $this->payload['registration_ids'] = array_unique($this->payload['registration_ids']); - sort($this->payload['registration_ids']); - - if (isset($this->payload['time_to_live']) && !isset($this->payload['collapse_key'])) - $this->payload['collapse_key'] = 'GCM Notifications'; - - $data = json_encode($this->payload); - return $this->request($data); - } - - - - - - - - protected function request($data) - { - - $headers[] = 'Content-Type:application/json'; - $headers[] = 'Authorization:key='.$this->apiKey; - - $curl = curl_init(); - - curl_setopt($curl, CURLOPT_URL, $this->apiSendAddress); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - curl_setopt($curl, CURLOPT_HEADER, true); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, $data); - - $this->responseData = curl_exec($curl); - - $this->responseInfo = curl_getinfo($curl); - - curl_close($curl); - - - - return $this->parseResponse(); - } - - - protected function parseResponse() - { - if ($this->responseInfo['http_code'] == 200) - { - $response = explode("\n",$this->responseData); - $responseBody = json_decode($response[count($response)-1]); - - if ($responseBody->success && !$responseBody->failure) - { - $message = 'All messages were sent successfully'; - $error = 0; - } - elseif ($responseBody->success && $responseBody->failure) - { - $message = $responseBody->success.' of '.($responseBody->success+$responseBody->failure).' messages were sent successfully'; - $error = 1; - } - elseif (!$responseBody->success && $responseBody->failure) - { - $message = 'No messages cannot be sent. '.$responseBody->results[0]->error; - $error = 1; - } - - $this->status = array( - 'error' => $error, - 'message' => $message - ); - - $this->messagesStatuses = array(); - foreach($responseBody->results as $key => $result) - { - if (isset($result->error) && $result->error) - { - $this->messagesStatuses[$key] = array( - 'error' => 1, - 'regid' => $this->payload['registration_ids'][$key], - 'message' => $this->errorStatuses[$result->error], - 'message_id' => '' - ); - } - else - { - $this->messagesStatuses[$key] = array( - 'error' => 0, - 'regid' => $this->payload['registration_ids'][$key], - 'message' => 'Message was sent successfully', - 'message_id' => $result->message_id - ); - } - } - - return !$error; - } - elseif ($this->responseInfo['http_code'] == 400) - { - $this->status = array( - 'error' => 1, - 'message' => 'Request could not be parsed as JSON' - ); - return false; - } - elseif ($this->responseInfo['http_code'] == 401) - { - $this->status = array( - 'error' => 1, - 'message' => 'There was an error authenticating the sender account' - ); - return false; - } - elseif ($this->responseInfo['http_code'] == 500) - { - $this->status = array( - 'error' => 1, - 'message' => 'There was an internal error in the GCM server while trying to process the request' - ); - return false; - } - elseif ($this->responseInfo['http_code'] == 503) - { - $this->status = array( - 'error' => 1, - 'message' => 'Server is temporarily unavailable' - ); - return false; - } - else - { - $this->status = array( - 'error' => 1, - 'message' => 'Status undefined' - ); - return false; - } - } - -} -