Skip to content

Instantly share code, notes, and snippets.

@sookcha
Created May 2, 2012 06:55
Show Gist options
  • Save sookcha/2574622 to your computer and use it in GitHub Desktop.
Save sookcha/2574622 to your computer and use it in GitHub Desktop.
Simple APNS Example
<?php
$deviceToken = 'YOUR DEVICE TOKEN'; // Device Token ID
$message = $_GET['url']; // Message
// Development
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsCert = 'apns-dev.pem';
// Production
//$apnsHost = 'gateway.push.apple.com';
//$apnsCert = 'apns-production.pem';
$apnsPort = 2195;
$payload = array('aps' => array('alert' => $message, 'badge' => 0, 'sound' => 'default'));
$payload = json_encode($payload);
$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);
if($apns)
{
$apnsMessage = chr(0).chr(0).chr(32).pack('H*', str_replace(' ', '', $deviceToken)).chr(0).chr(strlen($payload)).$payload;
fwrite($apns, $apnsMessage);
fclose($apns);
}
echo
(
"
Sending...
"
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment