sesc360
sesc360

Reputation: 3255

Apple Push Notification SSL Error

I got the following error message, when trying to send a push notif via PHP:

stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure

I guess it could be the issue with the SSL3 problems in the past? But does this mean the script is not usable that way anymore? What do I need to change, as i have no clue. I checked all certificates and they are working. I can connect to the sandbox at apple via terminal with the certificates and the handshake seems to work via terminal.

This is my PHP Script

class PushNotification {

    public function sendTestMessageToDevice($message){

        $devicetoken = Config::get('mfsconfig.PushNotificationTest.deviceToken');
        $passphrase = Config::get('mfsconfig.PushNotificationTest.passPhrase');

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

        // Open connection to APNS
        $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $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 a binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $devicetoken) . pack('n', strlen($payload)) . $payload;

        // Send to server
        $result = fwrite($fp, $msg, strlen($msg));

        if(!result) {
            echo 'Message not delivered' . PHP_EOL;
        } else {
            echo 'Message successfully delivered' . PHP_EOL;
        }

        fclose($fp);
    }

Upvotes: 2

Views: 848

Answers (1)

sesc360
sesc360

Reputation: 3255

I think I got the solution:

  • Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
  • Right click and choose Export 2 items
  • Choose the p12 format from the drop down and name it cert.p12.

Now covert the p12 file to a pem file:

$ openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts

After uploading the newly created pem file, everything works smoothly!

Upvotes: 1

Related Questions