Skip to content

Instantly share code, notes, and snippets.

@serhatsezer
Created December 20, 2016 15:00
Show Gist options
  • Save serhatsezer/2de4a755c312dc28bc230897f8974afd to your computer and use it in GitHub Desktop.
Save serhatsezer/2de4a755c312dc28bc230897f8974afd to your computer and use it in GitHub Desktop.
<?php
header('Content-Type: application/json; charset=UTF-8');
ini_set('default_socket_timeout', 900);
$message = "serhat- hobi push";
$nData = array();
$nData["type"] = "normal";
sendNotification("8ba6c6b6dafba6482fde94d33d2a53a0c7ca660409f42200527497111266d882", $message, $nData);
// 8ba6c6b6dafba6482fde94d33d2a53a0c7ca660409f42200527497111266d882 - other
// 5fcb668c2e4ee9637162fa166b5cfc966bfa94f2b3de4960bc29eca0518bfe73 - serhat
function sendNotification($token, $message, $nData) {
echo "---";
$pemName = "apns.pem";
$passphrase = 'push123';
$ctx = stream_context_create(array(
'http' => array(
'method' => 'GET',
'timeout' => 30, //<---- Here (That is in seconds)
)
));
stream_context_set_option($ctx, 'ssl', 'local_cert', $pemName);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit('Failed to connect: ' . $err . $errstr . PHP_EOL);
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'badge' => 1,
'sound' => 'default'
);
$body['data'] = $nData;
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) . $payload;
var_dump($msg);
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) {
echo "gonderemedi";
} else {
echo "gonderildi";
}
fclose($fp);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment