Skip to content

Instantly share code, notes, and snippets.

@marchbold
Created March 23, 2016 22:51
Show Gist options
  • Select an option

  • Save marchbold/393bd1b7828e30178899 to your computer and use it in GitHub Desktop.

Select an option

Save marchbold/393bd1b7828e30178899 to your computer and use it in GitHub Desktop.
A simple PHP script to send a test APNS push notification
<?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
@saelozahra
Copy link
Copy Markdown

saelozahra commented Feb 23, 2021

Hi Dear
When I run your code
return error:

Failed to connect: 0

And During change socket client link to

api.push.apple.com:443

return

Connected to APNS Message successfully delivered

but message not sent to phone

Thank you for your answer

@marchbold
Copy link
Copy Markdown
Author

This code uses the legacy process and probably will fail now. You need to set the cafile on the stream context now I believe.

@vilaniocf
Copy link
Copy Markdown

Please, i recept equals error... maybe it's there error on my example.

$tHost = 'api.push.apple.com';
$tPort = 443;

$tCert = __DIR__.'/passenger_apns_pro.pem';
$tPassphrase = '123456';
$tToken = "b9367feb5c0198562aef878d3e74484c203a51be49ad98ff0c3ec0eaaa6b2f33";

$tSound = 'default';

$tPayload = 'APNS payload';

$tBody['aps'] = array(

'apns-priority' => 10,

'badge' => +1,

'alert' => "Teste" .' ' ."texto teste",

'sound' => 'default'

);


$context_options = array (
    'https' => array (
        'method' => 'POST',
        'header'=> "Content-type: application/json",
        'content' => array($tCert,$tPassphrase)
        ),
    'ssl' => array('SNI_enabled'=>true,'SNI_server_name'=> $tHost)
);




$tBody['payload'] = $tPayload;

$tBody = json_encode($tBody);

$tContext = stream_context_create($context_options);

stream_context_set_option($tContext, 'ssl', 'local_cert', $tCert);

stream_context_set_option($tContext, 'ssl', 'passphrase', $tPassphrase);

$tSocket = stream_socket_client('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);

if (!$tSocket)

    exit ('APNS Connection Failed:' .$error. ' ' .$errstr . PHP_EOL);

$tMsg = chr(0).chr(0).chr(32).pack('H*', $tToken) .pack ('n', strlen($tBody)).$tBody;

// Send the Notification to the Server.

$tResult = fwrite($tSocket, $tMsg, strlen($tMsg));

if ($tResult)

    echo 'Delivered Message to APNS' . PHP_EOL. $tResult.PHP_EOL;

else

    echo 'Could not Deliver Message to APNS' . PHP_EOL;

// Close the Connection to the Server.

fclose ($tSocket);

@teffi
Copy link
Copy Markdown

teffi commented Apr 26, 2021

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.

@vilaniocf
Copy link
Copy Markdown

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.

@vilaniocf
Copy link
Copy Markdown

Captura de Tela 2021-04-26 às 12 02 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment