推送通知有时无效,有时可行。收到警告SSL3_WRITE_PENDING:错误的写入重试和SSL:管道损坏

时间:2017-04-07 10:43:29

标签: php ios ssl

$passphrase = '';
$deviceToken="8ebf42fd63c37071b9309f938a527189525b777f35571fc249ec1ed2581d3441";
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', "pushCert.pem");
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
{
exit("Failed to connect: $err $errstr" . PHP_EOL);
return 0;
}
// Create the payload body
$body['aps'] = array(
'notification_type_id'=>1,
'contest_id'=>1,
'pic_id'=>1,
'rid'=>1,
'alert' => "Hi John test push",
'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));
if (!$result)
{
echo 'Message not delivered' . PHP_EOL;
}else{
echo 'Message successfully delivered' . PHP_EOL;
} 

为什么有时会推送通知工作而有时却没有? 向IOS设备发送推送通知时,我收到的警告低于警告

  

严重性:警告

消息:fwrite():SSL操作失败   代码1. OpenSSL错误消息:错误:1409F07F:SSL   例程:SSL3_WRITE_PENDING:错误的写入重试

文件名:   models / cron_win_contest_model.php

行号:386

或者

  

严重性:警告

消息:fwrite():SSL:管道损坏

  

文件名:models / cron_win_contest_model.php

行号:   386

你能帮我解决这个问题吗

2 个答案:

答案 0 :(得分:0)

当您尝试写入套接字时。如果出现问题,fwrite将返回false0(取决于php版本)作为返回值。当它发生时,你必须管理它。

换句话说,在fwrite()之前添加sleep(5)。

您可以这样做: -

try {                           
    $result = fwrite($fp, $msg, strlen($msg));
}
catch (Exception $ex) {
    sleep(5); //sleep for 5 seconds
    $result = fwrite($fp, $msg, strlen($msg));
    if ($result)
    {
        return true;
    }
    else {
        return false;
    }
}

希望它有所帮助!

答案 1 :(得分:0)

我在生产中遇到了同样的问题,并按照以下步骤解决了问题

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

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

Step 2: add below line just before the "$result = fwrite($fp, $msg, trlen($msg));"

stream_set_blocking($fp, 0);