<?PHP
header("Content-type: application/json; charset=utf-8");
date_default_timezone_set('Asia/Jerusalem');
require_once('class.db.php');
if(!isset($_GET['user_id'])){
    $result1['status']= false;
            $result1['http_code']= (int) 200;
             $result1['msg']='user_id مطلوب';
            $result1['token']='';
            $result1['data']=array();
  print json_decode($result1,true);exit();
}else{
$user_id=(int) $_GET['user_id'];
$title= $_GET['title']; 
$body= $_GET['body'];
$breaks = array("<br />","<br>","<br/>"); 
  $body=str_replace($breaks, "\n", $body); 
  $title=str_replace($breaks, "\n", $title); 
$data=array(
      'title'=>$title,
      'body'=>$body
    );
  $db = new Db();
  $rows = $db -> select(" SELECT * FROM `user_firebaseToken` where user_id=$user_id");
  foreach ($rows as $val) {
  $to= $val['firebaseToken']; 
  $apiKey='AIzaSyCBY7DfjkXXcAZ2lcHQ5Usu1JquY_jQ4gA';
  
  $fields=array('to'=>$to,'notification'=>$data);
  
  $headers=array('Authorization:key='.$apiKey,'Content-Type: application/json');
  
  $url='https://fcm.googleapis.com/fcm/send';
  
   $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
	            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));
	            $result = curl_exec($ch);
	            curl_close($ch);
	
	          
  }
            $result1['status']= true;
            $result1['http_code']= (int) 200;
             $result1['msg']='send notification ';
            $result1['token']=$user_id;
            $result1['data']=array();
  return json_decode($result1,true);
}
?>