【问题标题】:SSL error when adding ios push notification code添加 ios 推送通知代码时出现 SSL 错误
【发布时间】:2018-06-15 14:18:02
【问题描述】:

我有一个应该发送 IOS 推送消息的代码,但它给了我一个错误。

我的代码:

$streamContextCreate = stream_context_create();

stream_context_set_option($streamContextCreate, 'ssl', 'local_cert', '/home/devmzad/public_html/public/ios/MzadDevCertificates.pem');

$fp = stream_socket_client(
    'ssl://gateway.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContextCreate);
echo "<pre>";
print_r($fp); //gives error here.
die;

我收到的错误如下:

stream_socket_client():SSL 操作失败,代码为 1。OpenSSL 错误消息: 错误:14094438:SSL 例程:SSL3_READ_BYTES:tlsv1 警报内部错误

如果有人可以帮助我,我将不胜感激。谢谢。

【问题讨论】:

    标签: php ios ssl apple-push-notifications


    【解决方案1】:

    请尝试以下代码:

    <?php
            /* We are using the sandbox version of the APNS for development. For production
            environments, change this to ssl://gateway.push.apple.com:2195 */
            $apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';
            /* Make sure this is set to the password that you set for your private key
            when you exported it to the .pem file using openssl on your OS X */
            $privateKeyPassword = '1234';
            /* Put your own message here if you want to */
            $message = 'Welcome to iOS 7 Push Notifications';
            /* Pur your device token here */
            $deviceToken =
            '05924634A8EB6B84437A1E8CE02E6BE6683DEC83FB38680A7DFD6A04C6CC586E';
            /* Replace this with the name of the file that you have placed by your PHP
            script file, containing your private key and certificate that you generated
            earlier */
            $pushCertAndKeyPemFile = 'PushCertificateAndKey.pem';
            $stream = stream_context_create();
            stream_context_set_option($stream,
            'ssl',
            'passphrase',
            $privateKeyPassword);
            stream_context_set_option($stream,
            'ssl',
            'local_cert',
            $pushCertAndKeyPemFile);
    
            $connectionTimeout = 20;
            $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
            $connection = stream_socket_client($apnsServer,
            $errorNumber,
            $errorString,
            $connectionTimeout,
            $connectionType,
            $stream);
            if (!$connection){
            echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
            exit;
            } else {
            echo "Successfully connected to the APNS. Processing...</br>";
            }
            $messageBody['aps'] = array('alert' => $message,
            'sound' => 'default',
            'badge' => 2,
            );
            $payload = json_encode($messageBody);
            $notification = chr(0) .
            pack('n', 32) .
            pack('H*', $deviceToken) .
            pack('n', strlen($payload)) .
            $payload;
            $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
            if (!$wroteSuccessfully){
            echo "Could not send the message<br/>";
            }
            else {
            echo "Successfully sent the message<br/>";
            }
            fclose($connection);
    
      ?>
    

    【讨论】:

    • 还要检查 2195 和 2196 端口是否打开,然后它将从 APNS 进行通信。
    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    相关资源
    最近更新 更多