Simple PHP Script to Send iOS Push Notifications

Updated on     Kisan Patel

Simple PHP script to send iOS Push Notification.

$tokens = '-YOUR DEVICE TOKEN-';
$url = '-URL-';
$message = 'Hey there!';

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsCert = __DIR__ . '/inc/certificate.pem'; //Full Path of Certificate
$apnsPort = 2195;
$apnsPass = '-PASSWORD-';

$payload['aps'] = array('alert' => $message, 'badge' => 0, 'sound' => 'default', 'url' => $url);
$output = json_encode($payload);
$tokens = pack('H*', str_replace(' ', '', $tokens));
$apnsMessage = chr(0).chr(0).chr(32).$tokens.chr(0).chr(strlen($output)).$output;

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $apnsPass);

$apns = stream_socket_client('ssl://'.$apnsHost.':'.$apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
fwrite($apns, $apnsMessage);
fclose($apns);

PHP

Leave a Reply