<?php
//<< HTTP v1 추가 코드
error_reporting( E_ALL );
ini_set( "display_errors", 1 );

include_once "/var/www/html/fcm/fcm.php";
//include_once "../fcm/fcm_test.php";
//>>


define( "GOOGLE_API_KEY", "AAAAS14VmNs:APA91bFAJZHMfxcjFlZEpmC-3f8GNpOKVDItusCedM1JuCqdQMo0vJV1nKGvVletzfXo9Oc0aEsTZJmtJcKtYeU8hofoBsd7Gv-v6dfnOUfcLXMb6tZY2utVDCiykwylC-Ki1Q1yfIdU8nhLMGuMleLiMW1doM56KA" );

define( "ONESIGNAL_APP_ID", "92a335be-2fbb-4957-937a-78a88d7f13e4" );
define( "REST_API_KEY", "Mzg4YmU2MWMtMzgwMy00ODVmLWEwN2QtNTdiNWRhMWFhOGJm" );


function send_notification( $tokens, $message ) {
    //<<[24.06.04] HTTP v1 추가 코드
    send_notification_fcm_httpv1($tokens, $message);
    //>>
    /*
	$url = 'https://fcm.googleapis.com/fcm/send';

	$fields = array(
		'registration_ids' => $tokens,
		'data' => $message
	);

	$headers = array(
		'Authorization:key =' . GOOGLE_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 );
	if ( $result === FALSE ) {
		return 'FCM Send Error: ' . curl_error($ch);
	}
	curl_close( $ch );
	return $result;
    */
};


function send_notification_ios($tokens, $message){
    $content = array(
        "en" => 'M세이버 인증'
        );


    $headings = array(
        "en" => "로그인이 감지되었습니다.",
    );
	


    $fields = array(
        'app_id' => ONESIGNAL_APP_ID,
        'include_player_ids' => array($tokens), // Play id from database
        'data' => array("foo" => "bar"),
        'headings' => $headings,//Title of your website
        'contents' => $content // the message
    );

    $fields = json_encode($fields);


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications/");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                               'Authorization: Basic '.REST_API_KEY));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//<<[2021.03.29] HTTPs2 방식 애플 푸시.
function send_APNS_HTTP2($title, $idx, $authtype, $locationtype, $otp, $code, $site, $param1, $param2, $param3, $location1, $location2, $location3, $location4, $distance, $token, $vid) {	
	$url = 'https://push.seowoosnc.com:443/cloud/push?idx='.$idx.'&authtype='.$authtype.'&token='.$token.'&otp='.$otp.'&code='.$code.'&site='.$site.'&title='.'MSABER'.'&param1='.$param1.'&param2='.$param2.'&param3='.$param3.'&locationtype='.$locationtype.'&location1='.$location1.'&location2='.$location2.'&location3='.$location3.'&location4='.$location4.'&distance='.$distance;
	
	$options = array("ssl"=>array("verify_peer"=>false, "verify_peer_name"=>false,),);
	json_decode(file_get_contents($url, false, stream_context_create($options)));
	
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


?>
