<?php
require_once('config.php');
require_once('f_insert_notification_log_send.php');
//$token = 'cU6vO63uSeS-2_nbu10BtT:APA91bGKAocNRzBskwJzTKgydZChxaYZ5dKXerbV9OiBYau9Q9FPFYk9rN_1AnJWXDy9aHIQa7KmMkXj-F-YIoWhPJaMJKzTwixTkz4UeD7c1tw5zy5l70yJuJa7q6nyljuL_l5cvJPf';
//$message = 'HI';
//$title = 'Work Order';
//$sitecode ='SMS';
//https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
//push_notification_android($token,$message,$title,$sitecode);



function push_notification_android($token,$message,$title,$sitecode,$tokenlist,$RowID,$row,$tkn_site,$LOGINID){
	
	
	//echo '<pre>'; print_r($token); echo '</pre>';
	//echo '<pre>'; print_r($RowID); echo '</pre>';
	//echo '<pre>'; print_r($tokenlist); echo '</pre>';
	
	/* echo $token[0];
	echo '   ';
	
	echo $token[1]; */
	//echo		$sitecode;	

    //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';
                
    $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;
	
	
	

}


?>