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?
source share