IOS中的推送通知 - 握手失败

时间:2017-09-15 11:22:13

标签: php ios push-notification apple-push-notifications

每当我尝试发送推送通知时,它会抛出多个错误, 我已经研究得很好但没有解决方案。

  

1.stream_socket_client():SSL操作失败,代码为1. OpenSSL错误消息:   错误:14094410:SSL例程:SSL3_READ_BYTES:sslv3警报握手失败

-

  

2.Severity:警告

      

消息:stream_socket_client():无法启用加密

-

  

3.Severity:警告

      

消息:stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(未知错误)

-

  

4.无法连接:0

以下是我的代码:

// Sends Push notification for iOS users
    public function iOS($data, $devicetoken) 
    {
        $deviceToken = $devicetoken;
        $passphrase= "";
        $ctx = stream_context_create();
        // ck.pem is your certificate file
        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);
        // Create the payload body
        $body['aps'] = array(
        'alert' => array(
        'title' => $data['mtitle'],
        'body' => $data['mdesc'],
        ),
        'sound' => 'default'
        );
        // 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));
        // Close the connection to the server
        fclose($fp);
        if (!$result)
            return 'Message not delivered' . PHP_EOL;
        else
            return 'Message successfully delivered' . PHP_EOL;
    }

任何人都可以帮我解决一下吗?

1 个答案:

答案 0 :(得分:0)

添加以下行

stream_context_set_option( $ctx , 'ssl', 'verify_peer', false); 

如果您在生成pem文件时设置了密码,那么您必须在以下行中提及

  $passphrase= "";

如果由于pem文件而没有设置此错误。它没有正确生成。