Home » Php » php – How to use Apple Push Notifications if you can't use port 2195?

php – How to use Apple Push Notifications if you can't use port 2195?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have developed a script using stream_socket_client() and it works on my localhost but when I try to use it online (with Fatcow.com web hosting) it won’t work. I receive the following error:

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195
(Connection refused) in …/send-notification.php on line 18
Failed to connect 111 Connection refused

This is line 18:

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

I know that the password for the .pem file is correct. The question becomes if I can’t use this port what do I do? Is there any free service that I can use? I know that port 80 is open.

I’ve heard that using the curl() function will work but have been unsuccessful so far.

I would really appreciate getting pointed in the right direction.

How to&Answers:

In order to send the notifications to the device need to enable 2195 port in the firewall.
For security reasons some organizations disabling these ports in the firewall.
2196 port is used for feedback services and this port also should open in the firewall.
Without opening these ports messages cannot deliver to APNS.

Answer:

I believe the following is what you are looking for

$url = 'https://gateway.sandbox.push.apple.com:2195';
$cert = 'AppCert.pem';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase");
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_tokens": ["458e5939b2xxxxxxxxxxx3"], "aps": {"alert": "test message one!"}}');

$curl_scraped_page = curl_exec($ch);

more information can be found here: Apple push notification with cURL

Answer:

try using port 2196.

I have also faced similar issue, even with cURL you will face this issue. Double check with your hosting provider whether outgoing port (2195,2196) are opened or blocked.
Or try to telnet ssl://gateway.sandbox.push.apple.com:2195
and check what response you get. If the ports are opened you will be connected.

Answer:

I had the same problem with my current hosting provider.
You could switch to another provider
– or –
Simply ‘buy’ a budget LAMP VPS (3 euro/month).
And you’re good to go

Answer:

in every shared hosting they disable 2195 port for that you can request to enable this port but in most of the shared hosting company dont enable this port so if you want to do ios push notification i recommended you to use Virtual Private Server(VPS) and you can easly configure ios push notification.