$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 设备发送推送通知时出现以下警告
Severity: Warning
Message: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry
Filename: models/cron_win_contest_model.php
Line Number: 386
或者
Severity: Warning
Message: fwrite(): SSL: Broken pipe
Filename: models/cron_win_contest_model.php
Line Number: 386
你能帮我解决这个问题吗
当您尝试写入套接字时。如果出现问题,fwrite 将返回 false
或 0
(取决于 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;
}
}
希望对您有所帮助!
我是一名优秀的程序员,十分优秀!