Reputation: 4160
I'm working on an iOS app that use iOS Push Notifications. I want to send the notification from a php script on my server. The code in my app works really good for registering for remote notification and receive them. I use this php script to send notification and it works well too:
<?php
// My device token here (without spaces):
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// My private key's passphrase here:
$passphrase = 'myPrivateKey';
// My alert message here:
$message = 'New Push Notification!';
//badge
$badge = 1;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
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,
'badge' => $badge,
'sound' => 'newMessage.wav'
);
// 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 'Error, notification not sent' . PHP_EOL;
else
echo 'notification sent!' . PHP_EOL;
// Close the connection to the server
fclose($fp);
My problem is that if i run this script by terminal on my imac, all works good and notifications are sent, but if i upload it on my server it doesn't work. My certificate is valid and it's in the same folder of the script. When i try to run the script on the server, i add this line to include the certificate file and use the variable $cerificate:
$certificate = file_get_contents('ck.pem');
This is the error of the php server side:
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in script.php on line 29
Failed to connect: 110 Connection timed out
Why this happens? Do i have to add something on my server to enable pans connection?
-----UPDATE QUESTION-----
My first problem was that i didn't have the privileges to send connection on port 2195 and receive response on port 2196. Now, my server admin enabled these connection and i have different errors:
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 script.php on line 28
Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in script.php on line 28
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in script.php on line 28
Failed to connect: 0
Upvotes: 7
Views: 26151
Reputation: 14477
It happens when you are using shared hosting, and hosting provider don't give you privileges to use outbound connections on port 2195 and 2196. You should use VPS or use some other providers like urbanAirship and parse.com for this. Or even you can run your own local server... for that
Upvotes: 6