<?php

// Put your device token here (without spaces):
$token = urldecode($_GET['token']);
$deviceToken = $token; // token dell'iPhone a cui inviare la notifica

// Passphrase for the private key (ck.pem file)
 $pass = "123";
// Get the parameters from http get or from command line
$message = $_GET['message'];
$badge = 1;
$sound = 'default';



$body = array();
$body['aps'] = array('alert' => array('title'=>$_GET['title'],'body'=>$message),'message' => $message);

if ($badge)
      $body['aps']['badge'] = $badge;
if ($sound)
      $body['aps']['sound'] = $sound;
$body['aps']['title'] = $_GET['title'];
//$body['aps']['request_id'] = $_GET['request_id'];
//$body['aps']['types'] = $_GET['types'];
//$body['aps']['geo'] = $_GET['get_geo_locaiton'];


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'valetproProd-apn.pem');

// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 120, STREAM_CLIENT_CONNECT, $ctx);

if (!$fp) {
      print "Failed to connect $err $errstrn";
      return;
} else {
      print "Connection OK\n";
}

$payload = json_encode($body);
$msg = chr(0).pack('n',32).pack('H*', str_replace(' ', '', $deviceToken)).pack('n',strlen($payload)).$payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);