Skip to content

Instantly share code, notes, and snippets.

@vinceyang15
Created November 13, 2014 07:20
Show Gist options
  • Save vinceyang15/ae346146d0ccfde4e86a to your computer and use it in GitHub Desktop.
Save vinceyang15/ae346146d0ccfde4e86a to your computer and use it in GitHub Desktop.
Test APNS script
<? php
$device = ''; // My iphone deviceToken
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default');
$payload['server'] = array('serverId' => 'localhost', 'name' => 'waach');
$output = json_encode($payload);
$apnsCert = 'apns-dev.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($output)) . $output;
fwrite($apns, $apnsMessage);
error_log(print_r($error, true));
error_log(print_r($errorString, true));
//socket_close($apns); seems to be wrong here ...
fclose($apns);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment