0

I've implemented Push-Notifications into my Project in Codeigniter(php) years ago and everything was working fine until 10 days ago. My .pem file is working fine on my local server as well as online notification tools.

Error : "stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Connection refused)".

I'm using the following function:

public function applePushSend($deviceToken, $msg){

            $mode ='prod';

            $ctx = stream_context_create();

            if($mode=='dev'){
                $gateway = 'ssl://gateway.sandbox.push.apple.com:2195';
                stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert_dev.pem');
            }else{
                $gateway = 'ssl://gateway.push.apple.com:2195';
                stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert_prod.pem');
            }

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

            if (!$fp){
            }else{

                $alert = array();
                $alert['title'] = "My Message";
                $alert['body'] = $msg;
                $body['aps']['alert'] = $alert;
                $body['aps']['badge'] = 1;
                $body['aps']['sound'] = 'default';
                $body['notificationtypeid'] = 4;

                $payload = json_encode($body);
                $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
                $pushresult = fwrite($fp, $msg, strlen($msg));
            }
            fclose($fp);
            return($pushresult);
        }
erikvimz
  • 5,256
  • 6
  • 44
  • 60
Pranky
  • 1
  • 2
  • Is it possible that your certificate has expired? This has instructions on how to check it using a .pem file: https://stackoverflow.com/questions/21297853/how-to-determine-ssl-cert-expiration-date-from-a-pem-encoded-certificate – dmgig Aug 27 '18 at 16:46
  • Only check the pem file path, and the port is on. – Madhuri Patel Aug 28 '18 at 09:31

1 Answers1

0

I change the code to:

$streamContext = stream_context_create(array('ssl' => array(
                      'local_cert' => $this->_sProviderCertificateFile
                    )));

The it works out like a charm!

Madhuri Patel
  • 1,270
  • 12
  • 24