无法连接到ssl://gateway.push.apple.com:2195(连接被拒绝)

时间:2018-08-27 12:45:49

标签: php codeigniter push-notification

几年前,我已经在Codeigniter(php)的项目中实现了Push-Notifications,并且直到10天之前,一切都工作正常。我的.pem文件在我的本地服务器以及在线通知工具上都可以正常工作。

错误:“ stream_socket_client():无法连接到ssl://gateway.push.apple.com:2195(连接  拒绝)”。

我正在使用以下功能:

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);
        }

1 个答案:

答案 0 :(得分:0)

我将代码更改为:

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

它像魅力一样发挥作用!