6

Disclaimer: on SO I found many similar questions and some answers but none solved my issue.

I have this easy PHP code:

<?php
    $deviceToken = "myDeviceToken";
    $message = "Hello from server";
    $badge = 1;
    $sound = "default";

    // Construct the notification payload
    $body['aps'] = array(
        'alert' => $message
    );

    if ($badge) {
        $body['aps']['badge'] = $badge;
    }

    if ($sound) {
        $body['aps']['sound'] = $sound;
    }

    /* End of Configurable Items */
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'my.pem');

    $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

    if (!$fp) {
        print "Failed to connect $err $errstrn";
        return;
    } else {
        print "Connection OK\n";
    }

    $payload = json_encode($body);
    $msg = chr(0) . pack("n", 32) . pack("H*", str_replace(" ", "", $deviceToken)) . pack("n", strlen($payload)) . $payload;
    print "sending message :" . $payload . "\n";
    fwrite($fp, $msg);
    fclose($fp);
?>

When called from browser I receive:

[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /home/srv/public_html/myTest.php on line 28

[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /home/srv/public_html/myTest.php on line 28

[13-Dec-2011 18:52:52] PHP Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /home/srv/public_html/myTest.php on line 28

Any idea of what's going on?

eloibm
  • 899
  • 2
  • 11
  • 27
AsTheWormTurns
  • 1,318
  • 3
  • 13
  • 26
  • http://stackoverflow.com/questions/7453015/ios-push-notification-problem-when-using-crontab-scheduler this link was the answer for the above issue –  Dec 30 '13 at 13:13

1 Answers1

8

Some random finding from the internet which could help:

  1. It may be a certificate problem. Try the stream options allow_self_signed and verify_peer to check that.

  2. Try to use explicitely sslv2:// or sslv3:// ?

  3. Permission problem on "/dev/urandom"

Savageman
  • 9,257
  • 6
  • 40
  • 50
  • it was a certificate problem, but far more stupid than the one solved by your point 1. The developer who produced the certificate inserted an unrequested password, without either mentioning he did. Thanks for the quality pointers. – AsTheWormTurns Dec 14 '11 at 13:07
  • this morning out of nowhere, we started to receive 'SSL operation failed with code 1' from our servers. Defining the ssl version for the APN server solved the problem. sslv3://... I think apple made a few changes on their servers. – emrahgunduz Aug 24 '12 at 12:49
  • Can you please give a complete php statement for your first point. I am mobile developer and does not know much of php. Also please explain your 3rd point. What is "dev/urandom"? – Geek Feb 25 '14 at 11:52
  • You can read the related PHP documentation here: http://php.net/manual/context.ssl.php Example included here: http://php.net/manual/function.stream-context-create.php – Savageman Feb 26 '14 at 13:06
  • Ask Developer to export certificate and key differently from keychain access. If they export both in one go this error will arise. – Gurjinder Singh Jul 21 '17 at 11:51