iOS推送通知: 连接被对等方重置

5
我正在尝试为苹果设备创建通知系统,但在尝试在服务器上运行时,我遇到了以下错误:
警告:stream_socket_client():SSL:连接被对等方重置,在/home/empresa/public_html/simplepush/push.php的第30行。 警告:stream_socket_client():无法启用加密,在/home/empresa/public_html/push/push.php的第30行。 警告:stream_socket_client():无法连接到ssl://gateway.sandbox.push.apple.com:2195(未知错误),在/home/empresa/public_html/push/push.php的第30行。连接失败:0
我的代码如下:
  <?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 to the server
fclose($fp);

可能发生了什么?谢谢。

我正在使用你发布的源代码中几乎完全相同的例程...它已经运行了数月,并快速向我们所有的iOS用户发送通知,但是我在几周前注意到它停止了...如果我对一两个设备进行测试,它确实可以发送成功...但是没有一个设备ID被报告为无效(我有一个自动从我们的mysql数据库中删除任何无效设备ID的过程)。你最终解决了这个问题吗? - tamak
在进行一些额外的测试时,所有设备ID都是有效的...它在随机ID处失败,因此看起来连接/流被中断了...(这解释了为什么当只有几个设备ID时它能正常工作...问题出现在发送通知消息的100多个设备时...所以我想我需要编写一个函数来连接APNS服务器,然后如果(在设备ID循环中)我发现没有连接,重新打开该连接并恢复发送?我不知道...仍在努力解决这个问题! - tamak
1
出现了相同的错误。你们有什么建议如何解决这个问题吗? - FaNtAMode
1个回答

1
我不能确定具体原因。
但请确保您没有做以下任何错误:
- 不要同时建立多个连接。可以重复使用同一个连接或在发送推送通知后关闭连接。实际上,服务器对于最大并行连接数有一定限制,一旦达到阈值,可能会给您带来麻烦。此外,苹果建议除非知道连接将处于空闲状态,否则应保持连接开放。 - 跨多个通知保持与APNs的连接打开;不要反复打开和关闭连接。APNs将快速连接和断开连接视为拒绝服务攻击。除非知道连接将长时间处于空闲状态,否则应保持连接开放。例如,如果您每天只向用户发送一次通知,则可以每天使用新连接。 - 不要将开发人员配置文件令牌发送到LIVE APNS。请将分发和开发应用程序令牌分开。如果尝试将沙盒令牌发送到LIVE APNS或反之亦然,则可能会导致错误。
来源-APNS - notifications push ios: Connection reset by peer PHP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接