Ios Push Notification does not work on the server

I tested the push notification on my local machine, it works fine. Then I uploaded my files to a real server with the same certificate (".pem" file), and I guarantee that ports 2195 and 2196 are open.

I tested:

telnet gateway.sandbox.push.apple.com 2195 

He works...

 root@server ~ # telnet gateway.sandbox.push.apple.com 2195 Trying 17.149.34.54... Connected to gateway.sandbox.push.apple.com. Escape character is '^]'. 

But when I test my php script, it returns:

Warning: stream_socket_client () [function.stream-socket-client]: cannot connect to ssl: //gateway.sandbox.push.apple.com: 2195 (Connection timeout)

Warning: stream_socket_client () [function.stream-socket-client]: could not connect to ssl: //feedback.sandbox.push.apple.com: 2196

Any suggestions?


This is the php code:

 $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', "path/to/certificate"); $fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $error, $errorString, 100, (STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT), $ctx); 
+4
source share
4 answers

This code will work fine:

  $device = 'sdfsdfsfsdffsd'; $payload['aps'] = array('alert' => 'Hello I am testing the server code ....', 'badge' => 1, 'sound' => 'default'); $payload = json_encode($payload); $options = array('ssl' => array( 'local_cert' =>'ck.pem', 'passphrase' => 'abc123' )); $streamContext = stream_context_create(); stream_context_set_option($streamContext, $options); $apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext); $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($payload)) . $payload; fwrite($apns, $apnsMessage); fclose($apns); 

Check only the path to the pem file and the port is enabled.

+5
source

I had the same problem getting "unable to connect to ... (connection timeout)". I can connect to my home machine, but I can not from the hosting server.

In my case, ports 2195 and 2196 were not just open from a hosted server. I had to contact technical support to open these ports. Ping may work because it uses a different port number. Therefore, please contact your hosting company to ensure that these ports are open.

Good luck

Kaz

+2
source

In the stream_socket_client function stream_socket_client you should not pass the path to the certificate, but the APNS URL.

This method opens the connection and returns it:

 public function connect(){ $streamContext = stream_context_create(); stream_context_set_option($streamContext, 'ssl', 'local_cert', '/your/cert/path'); $apns = stream_socket_client('use.apns.url.here', $error, $errorString, 9, STREAM_CLIENT_CONNECT, $streamContext); if (!$apns){ $this->logger->error("Failed to connect to APNS: {$error} {$errorString}."); } return $apns; } 

Once you have created your message using apns, you can click it using the following

 fwrite($apns, $apnsMessage); 

You can follow this tutorial http://goo.gl/9Q0u if your goal is to implement your own announcement notification API.

There are also existing php libraries:

And a very nice Java library (I am currently using it for massive clicks):

0
source

You must install your firewall to allow the entire block 17.0.0.0/8 (all of this belongs to Apple!). Check THIS ANSWER

And according to Apple :

APN servers use load balancing, so your devices will not always connect to the same public IP address for notifications. It’s best to allow access to these ports on the entire 17.0.0.0/8 address block that Apple has assigned.

If you are using a CSF firewall (e.g. me), I would recommend adding this line to the csf.allow file:

tcp|out|d=2195|d=17.0.0.0/8

Using this instead of "17.0.0.0/8" will only allow external connections to Apple and, in particular, to port 2195. NSA will not like it, but it is much more accurate and safer !;)

0
source

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


All Articles