The Controller code as defined below:
Code: Select all
protected function sendNotification($notification_id) {
$this->load->model('tool/image');
$notification_info = $this->db->query("SELECT * FROM ". DB_PREFIX ."mobiapp_notification mn LEFT JOIN ". DB_PREFIX ."mobiapp_notification_description mnd ON (mn.notification_id = mnd.notification_id) WHERE mnd.language_id = '". $this->config->get('config_language_id') ."' AND mn.notification_id = '". $notification_id ."'")->row;
$message = array(
'notification_id' => $notification_info['notification_id'],
'title' => $notification_info['title'],
'link' => $notification_info['link'],
'content' => $notification_info['content'],
'subTitle' => strip_tags(html_entity_decode($notification_info['content'])),
'bannerImage' => $notification_info['image'],
);
// Set POST variables
$gurl = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => "/topics/global",
'time_to_live' => 30,
'notification' => array (
'data' => $message,
'priority' => 'high',
'sound' => 'default',
),
//'data' => $message,
'delay_while_idle' => true
);
$headers = array(
'Authorization: key=' . $this->config->get('mobiapp_gcm_key'),
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $gurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
$this->load->model('mobiapp/notification');
if (isset(json_decode($result)->message_id)) {
$message_id = json_decode($result)->message_id;
$this->model_mobiapp_notification->sendNotification($message_id, $fields, $headers, $error = '');
} elseif (isset(json_decode($result)->error)) {
$error = json_decode($result)->error;
$this->model_mobiapp_notification->sendNotification($message_id = '', $fields, $headers, $error);
}
}
Code: Select all
public function sendNotification($message_id, $fields, $headers, $error) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_send SET message_id = '" . $message_id . "', fields = '" . $this->db->escape(serialize($fields)) . "', headers = '" . $this->db->escape(serialize($headers)) . "', error = '" . $error . "'");
}