I was able to successfully send a push notification to my testing device using this script before Apple made the change to the new APNs provider API in March, 2021.
<?php
$deviceToken = 'bed76ed5078e6b1302f680bfec69aa73ead7045e67c9cb6dac31466b90513c7b';
$passphrase = 'pushchat';
$message = 'Hello!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://api.sandbox.push.apple.com:443', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$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));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
fclose($fp);
What I have tried:
After Apple made the change to the new APNs provider, the script doesn't send a notification. I've read the User Notification documentation and created a new certificate signing request, registered a new App ID, obtained a new provider certificate from Apple, installed the certificate and private key, and tested that I can use my certificate to connect to APNS using this command:
openssl s_client -connect "${APNS_HOST_NAME}":443 -cert "${CERTIFICATE_FILE_NAME}" -certform DER -key "${CERTIFICATE_KEY_FILE_NAME}" -keyform PEM
I've also sent a push notification using this command with a successful HTTP/2 200 response:
curl -v --header 'apns-topic: com.faunna.pushchat' --header apns-push-type: alert --cert aps.cer --cert-type DER --key PushChatKey.pem --key-type PEM --data '{"aps":{"alert":"test"}}' --http2 https:
After reading the User Notification documentation and making the changes to the file to include the newly required header fields, specially method, and path, I copied the Header fields from Sending Notification Requests to APNs:
Apple Developer Documentation[
^]
<?php
HEADERS
- END_STREAM
+ END_HEADERS
:method = POST
:scheme = https
:path = /3/device/bed76ed5078e6b1302f680bfec69aa73ead7045e67c9cb6dac31466b90513c7b
host = api.sandbox.push.apple.com
apns-id = eabeae54-14a8-11e5-b60b-1697f925ec7b
apns-push-type = alert
apns-expiration = 0
apns-priority = 10
DATA
+ END_STREAM
$deviceToken = 'bed76ed5078e6b1302f680bfec69aa73ead7045e67c9cb6dac31466b90513c7b';
$passphrase = 'pushchat';
$message = 'Hello!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://api.sandbox.push.apple.com:443', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$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));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
fclose($fp);
The terminal response is:
Parse error: syntax error, unexpected token ":" in /Users/MacUser/Desktop/simple push folder/simplepush.php on line 6
Can you please help me correctly configure these header fields? It seems the message should be sent if these fields are correctly configured.