Post

Replies

Boosts

Views

Activity

APNS Push sent via custom server using Certificate-based authentication not arriving
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.
1
0
296
Sep ’24