Monday, September 3, 2018

PHP Sending Push Notifications Failed to connect: 111 Connection refused

Leave a Comment

I am trying to setup push notifications, I got my certificates from apple developer, unabled pushed notifications on my ios app and I have a PHP script that is suppose to send a push notification. My hosting provider whitelisted the port 2195 outbound for my account. But push notifications are still not working, I get this error

Failed to connect: 111 Connection refused

I am using Development Certificates and they are enabled in my apple developer account.

<?php   class PushNotifications {      private static $passphrase = 'PASSPHRASE';      public function iOS($data, $devicetoken) {          $deviceToken = $devicetoken;          $ctx = stream_context_create();          stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates.pem');         stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);          $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);         }          $body['aps'] = array('alert' => array('title' => $data['mtitle'], 'body' => $data['mdesc'],),'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));          fclose($fp);          if (!$result)             return 'Message not delivered' . PHP_EOL;         else             return 'Message successfully delivered' . PHP_EOL;      }      private function useCurl(&$model, $url, $headers, $fields = null) {          $ch = curl_init();         if ($url) {             curl_setopt($ch, CURLOPT_URL, $url);             curl_setopt($ch, CURLOPT_POST, true);             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);             if ($fields) {                 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);             }             $result = curl_exec($ch);             if ($result === FALSE) {                 die('Curl failed: ' . curl_error($ch));             }              curl_close($ch);              return $result;         }     }  } ?> 

Can anyone help?

3 Answers

Answers 1

Make sure you're not still getting blocked (run this from your php host):

telnet gateway.sandbox.push.apple.com 2195 

If that gets a connection refused, then the problem is still with your hosting provider or someone between you and APNS.

Or this, if you don't have shell access:

fsockopen('gateway.sandbox.push-apple.com.akadns.net', 2195); 

Answers 2

I've found the issue and fixed it.

The problem was in .pem certificate. Somehow there were two certificates in one file for both production and development .pem files. The same .pem file with two certificates was in the repo for a long time but APNs stopped to work only few months ago. Maybe something was upgraded/changed on the Apple's side.

However, the solution was to remove the second certificate from the .pem file. After that APNs started to work and they work now.

Answers 3

Try this hopefully, it's working for you.

Message Data

$pushnoti = array(); $pushnoti['type'] = 'message_notification'; $pushnoti['body'] = array( 'message' => "Your Order id Completed"); send_notification_iphone ($deviceToken, $pushnoti); 

Notification Function

function send_notification_iphone( $deviceToken, $data) {      // Put your private key's passphrase here:     $passphrase = self::$passphrase;     $pem = 'ck.pem';     $ctx = stream_context_create();     stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);     // 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);     $message['body'] = $data['body'];     $body['aps'] = array( 'alert' => $data['body']['message'], 'body' => $data['body'], 'type'=> $data['type'] );     $body['badge'] =3;     $body['sound'] ='cat.caf';     // 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         // Close the connection to the server         fclose($fp); } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment