<?php
require_once('config.php');
require_once('f_insert_notification_log_send.php');

function push_notification_android($token,$message,$title,$sitecode,$tokenlist,$RowID,$row,$tkn_site,$LOGINID){
	

    //API URL of FCM
    $url = 'https://fcm.googleapis.com/fcm/send';

    /*api_key available in:
    Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key*/    
	//$api_key = 'AAAAxts01pc:APA91bHSj3djoewOJUH3TIvpA7YPDb1uA7afDYPp4ZSYCD9OqyKtnVQl6gw-zvjcoUIKDNDG6uTs24V_iyzUY9fazNxGXgvdGAVKLsqbUxLzCS6SrxV5ijJnefGWLUUzzY1OySTDAMGi';
	//NEW REACT NATIVE API_KEY
	$api_key = 'AAAAUkan1GU:APA91bE5HevBwUu96UIej2z4EU69A6ZwQ-FliM_Ex_y0W6--qG6nD_5eRqa2mE-fpnD-Abcoh234FC4rpqUnMhWyCpwLD-HO0KXiTNWunWrL5z-9w2BzLcMhn2jc_qZ96oNRyczvcRo3';
    
                
    $fields = array (
        'to' => $token,
		"notification" => array ("title" => $title,"click_action" => "SOMEACTIVITY","icon" => "appicon","body" => $message),	
        'data' => array ("title" => $message,"message" => $sitecode,"activity" => 'Web')
    );

    //header includes Content type and api key
    $headers = array('Authorization:key='.$api_key,'Content-Type:application/json');
                
    $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_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
	//echo $result;
	//echo file_put_contents("Send_Notification.txt",$result);
    if ($result == FALSE) {
        //die('FCM Send Error: ' . curl_error($ch));
		
		$insert_ntf_send = insert_notification_log_send($sitecode, $row, $tkn_site, $tokenlist, $RowID, 'E', curl_error($ch),$LOGINID);
    }else{
		
		$insert_ntf_send = insert_notification_log_send($sitecode, $row, $tkn_site, $tokenlist, $RowID, 'S',  $result,$LOGINID);
	}
    curl_close($ch);
    return $result;
	
	
	

}


?>