<?php
class MSG{
    private $db;
    public function __construct($db)
    {
        $this->db  = $db;
    }
    function sendTo($emp)
    {
        $db = $this->db;
        $tok = $db->prepare("SELECT token FROM loginhistory where default_user = $emp");
        $tok->execute();
        $tokens = $tok->fetchAll(PDO::FETCH_ASSOC);
        return $tokens;
    }
function msgBody($title,$body,$tokens){
    foreach ($tokens as $token) {
        $registrationIds[] = $token['token'];
	}
    $fields = array(
        "registration_ids" =>$registrationIds,
        "notification"=>array(
            "body"=>$body,
            "title"=>$title,
            "icon" 	=> "img/icon.png",
		    "image" => "img/d.png",
            "click_action"=>"https://www.alwessam.bdr14.com",
        ),
        'vibrate'=>'1',
		'sound'=>'default',
    );
    return $fields ;
}
function sendMsg($title,$body,$emp){
    $db = $this->db;
    $msg = new MSG($db);
    $tokens = $msg->sendTo($emp);
    $fields = $msg->msgBody($title,$body,$tokens);
    
	$url = 'https://fcm.googleapis.com/fcm/send';
	$headers = array(
		'Authorization:key=AAAAsBL6lsg:APA91bHym5fpyS4QWc2ds4c6XdWs2ZXeyXz1b9JfC0GJ8d5hRAcGmvqUFFEgSuRDN5CVyj5l8bu5nrMRJAMZ6K-BrMxTfKWMbdxen5FlJjVailhRUTEvNJpduvn6StXdmf0FCRZpLh5g',
		'Content-Type:application/json'
    );   
	$fields = json_encode ( $fields );
    $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 );
        curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
        $result = curl_exec ( $ch );
        curl_close ( $ch );
        echo $result;
    }
}
include_once "../actions/dbPDO.php";
$msg_text = "مجرد تجربة الرسالة الكاملة";
$title = "تنبيه كاذب" ;
$emp = 3 ;
$msg_class = new MSG($db);
$rest = $msg_class->sendMsg($title,$msg_text,$emp);
echo $rest ;

?>