<?php
function sendIosTechAssignPush($title, $body, $token, $show_in, $task_id, $from, $user_id, $user_name, $tech_id, $tech_name) {
	/*
	// Put your device token here (without spaces):	
	$deviceToken = str_replace(" ", "", $token);
	// Put your private key's passphrase here:
	$passphrase = 'swaroop';
	//// Put your alert message here:
	$message = $title." - ".$body;
	$ctx = stream_context_create();
	stream_context_set_option($ctx, 'ssl', 'local_cert', BASE_URI.'lcontrol/includes/ckT.pem');
	//stream_context_set_option($ctx, 'ssl', 'local_cert', 'CKPdtnTech.pem');	
	stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);							   
	$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
								   $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
	// Create the payload body
	$push_body['aps'] = array('badge' => +1, 'alert' => $message, 'sound' => 'default');
	$push_body['open_page'] = $from;
	$push_body['task_id'] = $task_id;
	$push_body['user_id'] = $user_id;
	$push_body['user_name'] = $user_name;
	$push_body['tech_id'] = $tech_id;
	$push_body['tech_name'] = $tech_name;
	$payload = json_encode($push_body);

	// Build the binary notification
	$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
	// Send it to the server
	$result = fwrite($fp, $msg, strlen($msg));
	
	if (!$result) {
    	//echo 'Message not delivered' . PHP_EOL;
	} else {
    	//echo 'Message successfully delivered amar'.$message. PHP_EOL;
	}
	// Close the connection to the server
	fclose($fp);*/
}

function iosTechActivate($title, $body, $token, $show_in) {
	
	/*
	// Put your device token here (without spaces):	
	$deviceToken = str_replace(" ", "", $token);
	// Put your private key's passphrase here:
	$passphrase = 'swaroop';
	//// Put your alert message here:
	$message = $title." - ".$body;
	$ctx = stream_context_create();
	stream_context_set_option($ctx, 'ssl', 'local_cert', BASE_URI.'lcontrol/includes/ckT.pem');
	//stream_context_set_option($ctx, 'ssl', 'local_cert', 'CKPdtnTech.pem');	
	stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);							   
	$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
								   $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
	// Create the payload body
	$push_body['aps'] = array('badge' => +1, 'alert' => $message, 'sound' => 'default');
	//$push_body['open_page'] = 'task';	
	$payload = json_encode($push_body);

	// Build the binary notification
	$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
	// Send it to the server
	$result = fwrite($fp, $msg, strlen($msg));
	
	if (!$result) {
    	echo 'Message not delivered' . PHP_EOL;
	} else {
    	echo 'Message successfully delivered amar'.$message. PHP_EOL;
	}
	// Close the connection to the server
	fclose($fp);
	exit;*/
}


//sendIosTechPush("test", "testing", "fa0b4a8e c7405260 4d89f4c0 a0544485 414f7382 a34c0329 8bf0d101 643b1b52", "tech");
function sendIosTechPush($title, $body, $token, $show_in) {
	
	// Put your device token here (without spaces):
	/*
	$deviceToken = str_replace(" ", "", $token);
	// Put your private key's passphrase here:
	$passphrase = 'swaroop';
	//// Put your alert message here:
	$message = $title." - ".$body;

	$ctx = stream_context_create();
	stream_context_set_option($ctx, 'ssl', 'local_cert', BASE_URI.'lcontrol/includes/ckT.pem');
	//stream_context_set_option($ctx, 'ssl', 'local_cert', 'CKPdtnTech.pem');	
	stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

	//$fp = stream_socket_client('ssl://gateway.push.apple.com:2195',
							   //$err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);	
							   
	$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
								   $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

	// Create the payload body
	$push_body['aps'] = array('badge' => +1, 'alert' => $message, 'sound' => 'default');	
	$payload = json_encode($push_body);

	// Build the binary notification
	$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
	// Send it to the server
	$result = fwrite($fp, $msg, strlen($msg));
	
	if (!$result) {
    	//echo 'Message not delivered' . PHP_EOL;
	} else {
    	//echo 'Message successfully delivered amar'.$message. PHP_EOL;
	}
	// Close the connection to the server
	fclose($fp);*/
}

function sendGCMCategory($title, $body, $token, $show_in) {

    $url = 'https://fcm.googleapis.com/fcm/send';
	
	$dat = array('title'=>$title,
				 'body'=>$body,				 
				 'sound'=>'default',
				 'show_in' => $show_in);	
	
    $fields = array('to' => $token,					
					'data' => $dat);
	
    $fields = json_encode ( $fields );

    /*$headers = array (
            'Authorization: key=' . "AAAAvAsjYuM:APA91bHhQVbDG9ZxoqNeP8ft0KjZOdnjmkXqVnEUTkjfm7GqmlfnTlIMK3zb27BzuGsTvB43TELgkNtRN34FtnG3cXGC06MISGmNViK6mhuaqz0k204Bm55ZNt_vgmuPDr_gK6f1HuGR",
            'Content-Type: application/json'
    );*/
    $headers = array (
            'Authorization: key=' . FCM_TECH_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_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
	/*echo '<pre>';
    print_r($result);
	exit;*/
    curl_close ( $ch );
}

function sendGCM($title, $body, $token, $show_in, $task_id, $user_id, $user_name, $type) {

    $url = 'https://fcm.googleapis.com/fcm/send';
	
	$dat = array('title'=>$title,
				 'body'=>$body,				 
				 'sound'=>'default',
				 'show_in' => $show_in,
				 'task_id'=>$task_id,
				 'user_id'=>$user_id,
				 'user_name'=>$user_name,
				 'type'=>$type);	
	
    $fields = array('to' => $token,					
					'data' => $dat);
	
    $fields = json_encode ( $fields );

    /*$headers = array (
            'Authorization: key=' . "AAAAvAsjYuM:APA91bHhQVbDG9ZxoqNeP8ft0KjZOdnjmkXqVnEUTkjfm7GqmlfnTlIMK3zb27BzuGsTvB43TELgkNtRN34FtnG3cXGC06MISGmNViK6mhuaqz0k204Bm55ZNt_vgmuPDr_gK6f1HuGR",
            'Content-Type: application/json'
    );*/
    $headers = array (
            'Authorization: key=' . FCM_TECH_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_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
	/*if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){
	echo '<pre>';
    print_r($result);
	exit;
	}*/
    curl_close ( $ch );
}

function sendGCM_user($title, $body, $token, $task_id, $from, $user_id, $user_name, $tech_id, $tech_name) {
		
	$url = 'https://fcm.googleapis.com/fcm/send';
	
	$dat = array('title'=>$title,
				 'body'=>$body,
				 'open_page'=>$from,
				 'task_id'=>$task_id,
				 'user_id'=>$user_id,
				 'user_name'=>$user_name,
				 'tech_id'=>$tech_id,
				 'tech_name'=>$tech_name,
				 'sound'=>'default');	
	
    $fields = array('to' => $token,					
					'data' => $dat);
		

//	$fields = array('registration_ids' => array($token),
//					'data' => $dat);
	
	$fields = json_encode ($fields);	
	
   	/*$headers = array (
            'Authorization: key=' . "AAAAbldmHG0:APA91bHoy3t5-C56ctQFFSUI4zPt1MwkblO8jG4ZbZoiNxI6QPkr-a0B7Dq5ErLXueScR7POJcUnHBkD3EDlThkcTU2zNXV5lOvNaDm6Wn9pOPsIN0ySBKeqwrXjLE9kJPlrxa1SuD4N",
            'Content-Type: application/json'
    );*/
    $headers = array (
            'Authorization: key=' . FCM_USER_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_POSTFIELDS, $fields );
    $result = curl_exec ($ch);
//	echo '<pre>';
//    print_r($result);
//	exit;
    curl_close ($ch);
}

?>