Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manjeshpv/e68f898d415af549705b to your computer and use it in GitHub Desktop.
Save manjeshpv/e68f898d415af549705b to your computer and use it in GitHub Desktop.
PHP: Apple APNS Push Notification for IOS Phonegap
<?php
// To Generate PEM file follow : http://stackoverflow.com/questions/21250510/generate-pem-file-used-to-setup-apple-push-notification
$deviceToken = '29954cd9ace7a7c29f66918e62e8a18522619c5cabae08972da6cd4273fe874c';
$passphrase = 'apns';
$message = 'test';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$body['aps'] = array('alert' => $message,'sound' => 'default');
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment