Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused)

Tags:

php

ios

ssl

I have a problem, I'd like to send out push notifications using php, however I keep getting this error:

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/colupon/public_html/iPhone/push/index.php on line 21
Failed to connect: 111 Connection refused

My code is as follows:

$deviceToken = '0f************************************************************78';

$passphrase = '************';

$message = 'My first push notification!';

////////////////////////////////////////////////////////////////////////////////

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

// Open a connection to the APNS server
$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 the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . 

$payload;

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

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

// Close the connection to the server
fclose($fp);

?>

I believe the problem is with the server I'm using because I posted the same code and the same .pem file on a different server and it sent the notification without any problem. I attempted to open up ports on the firewall for my server because I read that might cause this problem, but the same error message still showed up. Is there anything else I can do? Any help would be greatly appreciated, thanks!

like image 990
shadowarcher Avatar asked Nov 08 '13 18:11

shadowarcher


3 Answers

This issue is common problem in Apple Push notification, to resolve this error you have to go through as follows:

  1. Test your pem files locally and remotely in server if the problem is server go to 2 else build pem correct file.

  2. Set up the permission wrong on the folder that had the certificate file. This worked for me:

chmod 755 your_folder_that_has_certificate_files

3.Check connectivity of apns port 2195 from your hosting server as follows:

run

 telnet gateway.push.apple.com 2195

if this is the problem

Trying 17.172.233.36...
telnet: connect to address 17.172.233.36: Connection refused

you can solve this issue by opening the port 2195 on the production server. You can verify by following command $telnet gateway.push.apple.com 2195

-bash-3.2# telnet gateway.push.apple.com 2195

Trying 17.149.38.141...
Connected to gateway.push.apple.com (17.149.38.141).
Escape character is '^]'.
Connection closed by foreign host. 
like image 109
HMagdy Avatar answered Nov 14 '22 01:11

HMagdy


In case of MAC, (built-in server was working fine using terminal but not through browser, for me, so i installed MAMP.)

1.Go to---> /Library/WebServer/Documents/----copy both php and ckdev.pem file here.

2 go to terminal-->$open /private/etc-->go to--->apache2>originals>httpd.config file--> **"#LoadModule php5_module libexec/apache2/libphp5.so", remove "#"..(perhaps, you would have to change the permission also..!)

then goto browser and check--> localhost/yourPhpFile.php

In Case of Windows system,

1.Install WAMP,

2.goto php.ini file--->search for this ";extension=php_openssl.dll" line and remove semicolon ";".

3.click WAMP icon from right-bottom goto PHP>PHP Extensions>select php_openssl..

That's it..hope this may help further seekers.

like image 27
mavericks Avatar answered Nov 14 '22 01:11

mavericks


In my case the problem was that I forgot to setup my passphrase correctly in php script.

<?php

// Put your device token here (without spaces):
$deviceToken = 'xxx';

// Put your private key's passphrase here:
$passphrase = 'xxx';
like image 1
Denis Kutlubaev Avatar answered Nov 14 '22 02:11

Denis Kutlubaev