<?php 

	// Sends Push notification for Android users
	function android($data, $reg_id, $topic) {
	    $url = 'https://fcm.googleapis.com/fcm/send';
	    $API_ACCESS_KEY = "AAAA7Ln6vL8:APA91bGnAO-0H-BxmmV4M0RGjNjEhfzpMvkU9ZYXSLf8eBm8bFIlTWa5WYCBbMDEUtUdTv1NZduDKLj67AEkjFzoicH1nGVW9EFHVaxpIjvJaEngv4W9-tCjaVkt5P4BOTfQd1YshbGC";

		$headers = array(
	    	'Authorization: key=' . $API_ACCESS_KEY,
	    	'Content-Type: application/json'
	    );
		
		$message = array(
		    'title' => $data['title'],
		    'body' => $data['message'],
		    'icon' => "ic_notification",
		    'priority' => "high"
		    );
		    
		$fields = array(
		    'notification' => $message,
		    'time_to_live' => 2419200,
		    'delay_while_idle' => true,
		    );
		    
		if(isset($topic)){
		  $fields['to'] = "/topics/" . $topic;
		}
		else{
		    $fields['registration_ids'] = $reg_id;
		}
	
	    return useCurl($url, $headers, json_encode($fields));
	}
    	
    // Sends Push notification for IOS users
	function iOS($data, $reg_id, $topic) {
	    
	    $url = 'https://fcm.googleapis.com/fcm/send';
	    $API_ACCESS_KEY ="AAAA7Ln6vL8:APA91bGnAO-0H-BxmmV4M0RGjNjEhfzpMvkU9ZYXSLf8eBm8bFIlTWa5WYCBbMDEUtUdTv1NZduDKLj67AEkjFzoicH1nGVW9EFHVaxpIjvJaEngv4W9-tCjaVkt5P4BOTfQd1YshbGC";
		
		$headers = array(
	    	'Authorization: key=' . $API_ACCESS_KEY,
	    	'Content-Type: application/json'
	    );
		
		$message = array(
		    'title' => $data['title'],
		    'body' => $data['message'],
		    'contentAvailable' => true,
		    'priority' =>'high',
		    'sound' => 'default'
		    );
		    
		$fields = array(
		    'notification' => $message,
		    'time_to_live' => 2419200,
		    'delay_while_idle' => true,
		    'contentAvailable' => true,
		    );
		    
		if(isset($topic)){
		  $fields['to'] = "/topics/" . $topic;
		}
		else{
		    $fields['registration_ids'] = $reg_id;
		}
		
	    return useCurl($url, $headers, json_encode($fields));
    }
		
	// Curl 
	function useCurl($url, $headers, $fields = null) {
	        // Open connection
	        $ch = curl_init();
	        if ($url) {
	            // Set the url, number of POST vars, POST data
	            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);
	            if ($fields) {
	                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
	            }
	     
	            // Execute post
	            $result = curl_exec($ch);
	            if ($result === FALSE) {
	                die('Curl failed: ' . curl_error($ch));
	            }
	     
	            // Close connection
	            curl_close($ch);
	
	            return $result;
        }
    }

?>