Current File : /home/giriqfky/public_html/Barga/application/libraries//Fcmpushnotifications.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Fcmpushnotifications
{
var $API_key;
var $url;
var $app_icon;
public function __construct()
{
$this->API_key = FCM_API_KEY;
$this->url = FCM_URL;
$this->app_icon = FCM_APP_ICON;
}
public function send($tokens = array(), $message, $title = 'FLYNGENER', $picture_path = '', $platform = 'A', $noti_id = 0)
{
if(count($tokens) > 0)
{
$data = array(
"title" => $title,
'message' => $message,//text
'image' => $this->app_icon, //not worked in MI
"soundname" => "default",
"style"=> ($picture_path != '')?'picture':'inbox', //inbox
"summaryText"=> "There are %n% notifications",
"vibrationPattern"=> array(2000),
'picture' => ($picture_path != '')?$picture_path:'',
'summaryText'=> $message,//text
'msg' => $message, //text
'type'=> $picture_path != '' ? 'picture' : 'inbox',
'body' => $message,
'notId' => time()
);
//$data = array('title' => $title, 'body' => $message);
$fields['data'] = $data;
//$fields['notification'] = $data;
//print_r($tokens);
if($platform == 'I')
{
$fields['notification'] = array('body' => $message,
'title' => $title,
'sound' => "default",
'badge' => '1'
);
}
else
{
$fields["content_available"] = true;
}
if(is_array($tokens)){
$fields['registration_ids'] = $tokens;
}else{
$fields['to'] = $tokens;
}
// $fields['image'] = 'https://s-media-cache-ak0.pinimg.com/originals/ba/3c/dd/ba3cddc6ceb40f932d52d258c23b1f4f.png';
$fields['priority'] = 2;
$fields["time_to_live"] = 60*60*10;
return $this->curlRequest($fields, $noti_id);
}
}
private function curlRequest($fields, $noti_id)
{
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$this->API_key
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
insertNotificationResponse($noti_id, $fields, $result);
if ($result === FALSE) {
die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
}