Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@saturngod
Created January 24, 2013 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saturngod/4618649 to your computer and use it in GitHub Desktop.
Save saturngod/4618649 to your computer and use it in GitHub Desktop.
apple sample push notification
<?php
$deviceToken = '0a7d65e6 b621fbf2 7fb43b23 cb673af5 d689fdfa d2f9a8b2 59d2f507 f1e72a9c';
$payload['aps'] = array('alert' => 'push message suppose to be here but not here yet', 'title' =>'Im Title', 'badge' => 1, 'sound' => 'default', 'category' => 'notification');
$payload = json_encode($payload);
echo $payload."<br/>";
$apnsHost = 'gateway.sandbox.push.apple.com';//sandbox for development testing. For production , must use gateway.push.apple.com
$apnsPort = 2195;
//openssl pkcs12 -in server_certificates_bundle_sandbox.p12 -out server_certificates_bundle_sandbox.pem -nodes -clcerts
$apnsCert = dirname(__FILE__).'/mykey.pem';//read how to create pem key at http://code.google.com/p/apns-php/wiki/CertificateCreation
echo $apnsCert."<br/>";
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
echo "Show Error";
echo $error."<br/>";
echo $errorString."<br/>";
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
@socket_close($apns);
fclose($apns);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment