Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

// Put your device token here (without spaces):
$deviceToken = 'bed76ed5078e6b1302f680bfec69aa73ead7045e67c9cb6dac31466b90513c7b';
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = 'Hello!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$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;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
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://api.sandbox.push.apple.com/3/device/bed76ed5078e6b1302f680bfec69aa73ead7045e67c9cb6dac31466b90513c7b


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
// Put your device token here (without spaces):
$deviceToken = 'bed76ed5078e6b1302f680bfec69aa73ead7045e67c9cb6dac31466b90513c7b';
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = 'Hello!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$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;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
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.
Posted
Updated 31-Oct-21 20:51pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900