[英]APNS - notifications push ios: Connection reset by peer PHP
我的推送通知工作正常。 但有时,它从无处开始给出错误:
stream_socket_client(): SSL: 连接被对端重置
奇怪的是我不需要做任何事情来解决它,但等待。 过了一段时间,它又开始工作了。
我知道这是许多问题的重复,例如: notifications-push-ios-connection-reset-by-peer但它们都不能解决我的问题。
我正在使用 PHP stream_socket_client 生成套接字连接
使用中的代码是:
<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
$deviceToken= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$passphrase = ' ';
$message = 'my first notification';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
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);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'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));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection
}
我不能真正地指出主要原因。
但是请确保您没有做错以下任何事情:
在多个通知中保持与APN的连接打开; 不要反复打开和关闭连接。 APNs将快速连接和断开视为拒绝服务攻击。 除非您知道连接将长时间闲置,否则应将连接保持打开状态;例如,如果您每天仅向用户发送一次通知,则可以每天使用一个新连接。
在这个问题上苦苦挣扎了一段时间。 对于我的情况,结果是:
a) 和过期的 APNS 证书。 只有很小一部分错误解释说这是一个过期的证书。 大多数是“由对等方重置连接”。
b) 损坏的 APNS 证书。 只有在重新生成证书后它才开始工作。 在那之前,我再次遇到臭名昭著的“对等方重置连接”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.