APN php code gives a warning: stream_socket_client () [function.stream-socket-client]: cannot connect to ssl: //gateway.sandbox.push.apple.com: 2195

I am trying to implement an Apple Push notification using php code. Here is my code

$deviceToken = 'My device token'; $passphrase = ''; $message = 'My first push notification!'; //////////////////////////////////////////////////////////////////////////////// $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev-cert.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 120, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; $body['aps'] = array( 'alert' => $message, 'sound' => 'default' ); $payload = json_encode($body); $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; fclose($fp); 

The .pem certificate file is in the same directory as the file. This code works fine on my local machine. I am using MAMP. I get a notification on my devices.

But when I try to use it on the server, it does not work and gives an error.

Warning: stream_socket_client () [function.stream-socket-client]: cannot connect to ssl: //gateway.sandbox.push.apple.com: 2195 (Connection refused) at / home / nextgen / public _html / ApplicationGenerator / appointmentportal / iosapp / SimplePush / simplepush.php on line 14 Connection failed: 111 Disconnected

If the certificate file is incorrect, how will it work on my local server?

I get no way out of this. Can you guys help me?

+2
source share
1 answer

Assuming your server has the correct ports, "Connection Refused" usually indicates an invalid .PEM file or an invalid passphrase. Make sure that when opening the .PEM file, the header looks something like this:

 Bag Attributes friendlyName: Apple Development IOS Push Services: com.yourapp.app localKeyID: A8 77 BC 0C 2E 81 10 6E 78 9F XX XX XX XX XX XX 

subject = / UID = com.yourapp.app/CN = Apple Development IOS Push Services: com.yourapp.app/C=FR issuer = / C = US / O = Apple Inc./OU=Apple Worldwide Developer Relations / CN = Worldwide Partner Relationship Certification Authority

followed by a key, followed by a heading that looks like this for your private key:

 Bag Attributes friendlyName: Joe Black localKeyID: A8 77 BC 0C 2E 81 10 6E 78 9F XX XX XX XX XX XX XX XX Key Attributes: <No Attributes> 

I suggest that you also remove the passphrase to reduce potential sources of error.

+4
source

Source: https://habr.com/ru/post/987185/


All Articles