<?php
	define( 'API_ACCESS_KEY_CUST', "AIzaSyDe1c3Vt0jzCzlpWI1Uey0H8Puyyis4zng" );
	include("db.php");
	function WriteLog($string) {
		$myFile 	= "logs/log_".date("Y-m-d").".txt";
		$fh 		= fopen($myFile, 'a') or die("can't open $myFile file");
		$current 	= date("Y-m-d H:i:s");
		$stringData = "$current : $string\r\n";
		fwrite($fh, $stringData);
		fclose($fh);
	}
	$sms = $_REQUEST['msg'];
	$user_id = $_REQUEST['user_id'];        
	$query = "select * from pushnotification where user_id='$user_id'";
	$res= mysql_query($query) or die($query);
	$registrationIds = array();
	while($row = mysql_fetch_assoc($res)){
		$registrationIds[] = $row['token_id'];
	}
	if(count($registrationIds)>0){
		$msg1 = $sms;
		//$msg1 = str_replace("/"," ",$msg1);
		$msg = array();
		$msg = array(
			'message' 	=> '{"MessageType":"alert_notification","message":"'.$msg1.'"}'
		);			

		echo "Receved Msg in Your Device : <br>";
		var_dump($msg);
		$fields = array
		(
			'registration_ids' 	=> $registrationIds,
			'data'			=> $msg
		);
		WriteLog(print_r($fields,true));
		$headers = array
		(
			'Authorization: key=' . API_ACCESS_KEY_CUST,
			'Content-Type: application/json'
		);
		 
		$ch = curl_init();
		curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
		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, json_encode( $fields ) );
		$result= "";
		echo $result = curl_exec($ch );			
		curl_close( $ch );
	}
	die("DONE");

?>