Created
March 23, 2016 22:51
-
-
Save marchbold/393bd1b7828e30178899 to your computer and use it in GitHub Desktop.
A simple PHP script to send a test APNS push notification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Put your device token here (without spaces): | |
$deviceToken = 'DEVICE_TOKEN'; | |
// Put your private key's passphrase here: | |
$passphrase = 'passphrase'; | |
$pemfilename = 'ck.pem'; | |
// SIMPLE PUSH | |
$body['aps'] = array( | |
'alert' => array( | |
'title' => "You have a notification", | |
'body' => "Body of the message", | |
), | |
'badge' => 1, | |
'sound' => 'default', | |
); // Create the payload body | |
//////////////////////////////////////////////////////////////////////////////// | |
$ctx = stream_context_create(); | |
stream_context_set_option($ctx, 'ssl', 'local_cert', $pemfilename); | |
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); // Open a connection to the APNS server | |
if (!$fp) | |
exit("Failed to connect: $err $errstr" . PHP_EOL); | |
echo 'Connected to APNS' . PHP_EOL; | |
$payload = json_encode($body); // Encode the payload as JSON | |
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Build the binary notification | |
$result = fwrite($fp, $msg, strlen($msg)); // Send it to the server | |
if (!$result) | |
echo 'Message not delivered' . PHP_EOL; | |
else | |
echo 'Message successfully delivered' . PHP_EOL; | |
fclose($fp); // Close the connection to the server | |
As Marchbold shared due to recent Apple changes this needs to be refactored and seems like this won't be updated soon. I found an alternative which you guys might want to checkout
https://github.com/onmyway133/PushNotifications/ - An open source electron app which underneath uses a node library called APN.
Oh Tankyou, i tested my cert.pem w/ phrass and on token of my device on production mode, and i recept the notification normaly... but on my php like this example not.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please, i recept equals error... maybe it's there error on my example.
$tHost = 'api.push.apple.com';
$tPort = 443;