I have been trying to implement APNS Push Notifications into the iOS App I'm currently developing, and I'm having issues with notifications sent via my custom server not being delivered. I have built and ran the App on my device (iPhone running iOS 17.6.1), requested permission for notifications from UNUserNotificationCenter, and saved the device token for testing purposes.
As a sanity check for the device token and the App entitlements, I used the CloudKit tool for testing Push Notifications. The notifications I send aimed at the "DEVELOPMENT" Environment work properly and arrive on the device.
To configure my custom server, I created the Sandbox Apple Push Notifications certificate in my Developer portal, installed it on my Mac, exported the certificate and key together into a .p12 file, and created the PEM file using this command:
openssl pkcs12 -in Certificates.p12 -out sandbox.pem -nodes -clcerts
When my server attempts to send a notification, it appears to connect to the server and send the payload, but the notification never arrives on the device. I don't get any errors when my server writes the notification's binary payload to the connection.
For clarity, here is the code from my server that sends the "aps" payload to APNS (written in PHP):
public function sendPushNotification($deviceToken, $title, $message)
{
$jsonPayload = $this->composePayload($title, $message);
$msg = $this->buildBinaryNotification($jsonPayload, $deviceToken);
$this->connectToApns();
$result = fwrite($this->mApnsConnectionHandle, $msg, strlen($msg));
if (!$result)
{
$this->writeToLog("Message not delivered?!");
} else {
$this->writeToLog("Message successfully delivered!");
}
$this->disconnectFromApns();
}
And this is the code for constructing the JSON payload, and for converting that JSON into a binary string:
private function composePayload($title, $message)
{
// Create the payload body
$body['aps'] = array(
'alert' => array(
'title'=>$title,
'body'=>$message
)
);
// Encode the payload as JSON
$payload = json_encode($body);
return $payload;
}
private function buildBinaryNotification($payload, $deviceToken)
{
if (strlen($payload) > 0 && strlen($deviceToken) > 0)
{
$msg = chr(0) . pack('n', strlen($deviceToken)) . pack('H*', $deviceToken) . chr(0) . pack('n', strlen($payload)) . $payload;
}
return $msg;
}
Any suggestions or advice would be appreciated.
What you seem to be doing is creating a binary payload which is no longer supported. And you are probably also using the legacy Binary Interface which was retired March 31st, 2021 (https://developer.apple.com/news/?id=c88acm2b). Even you are using the correct current interface, your payload would be invalid and your notifications will be dropped.
You need to change your push servers to use the HTTP/2 API. Any push servers still using the legacy interface or payload will be unable to send notifications.
More information about the HTTP/2 provider API can be found in these two WWDC sessions:
- WWDC2015 Whats New in Notifications <https://developer.apple.com/videos/play/wwdc2015/720/&gt;
- WWDC2016 Whats New in the Apple Push Notification Service <https://developer.apple.com/videos/play/wwdc2016/724/&gt;
You can read more about the new APNs Provider API here: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns/
Argun Tekant / DTS Engineer / Core Technologies