Skip to content

Instantly share code, notes, and snippets.

@sudhanshuraheja
Created May 5, 2010 11:00
Show Gist options
  • Save sudhanshuraheja/390637 to your computer and use it in GitHub Desktop.
Save sudhanshuraheja/390637 to your computer and use it in GitHub Desktop.
<?php
// Create a stream to the server
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'apns-dev.pem');
$apns = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195,
$error,
$errorString,
60,
STREAM_CLIENT_CONNECT, $streamContext);
// You can access the errors using the variables $error and $errorString
$message = 'You have just pushed data via APNS';
// Now we need to create JSON which can be sent to APNS
$load = array(
'aps' => array(
'alert' => $message,
'badge' => 1,
'sound' => 'default'
)
);
$payload = json_encode($load);
// The payload needs to be packed before it can be sent
$apnsMessage = chr(0) . chr(0) . chr(32);
$apnsMessage .= pack('H*', str_replace(' ', '', $token));
$apnsMessage .= chr(0) . chr(strlen($payload)) . $payload;
// Write the payload to the APNS
fwrite($apns, $apnsMessage);
echo "just wrote " . $payload;
// Close the connection
fclose($apns);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment