Skip to content

Instantly share code, notes, and snippets.

@appyaaron
Created August 13, 2014 08:02
Show Gist options
  • Save appyaaron/894c8a9d0a58920336dd to your computer and use it in GitHub Desktop.
Save appyaaron/894c8a9d0a58920336dd to your computer and use it in GitHub Desktop.
Send a push
function sendAPNSSandbox($vDeviceToken, $message) {
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'Createanet');
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) return false;
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => 1
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $vDeviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) return false;
fclose($fp);
}
function sendAPNSProduction($vDeviceToken, $message) {
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns/ckprod.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'Createanet');
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) return false;
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => 1
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $vDeviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) return false;
fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment