gpt4 book ai didi

php - APNS - 通知推送 ios : Connection reset by peer PHP

转载 作者:行者123 更新时间:2023-12-01 20:09:43 26 4
gpt4 key购买 nike

我的推送通知工作正常。但有时,它从无处开始给出错误:

stream_socket_client():SSL:连接由对等方重置

Weird thing is i don't have to do anything to resolve it but wait. After sometime, it starts working back again.

我知道这是许多问题的重复,例如: 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
}

最佳答案

我真的无法指出其中的主要原因。

但请确保您没有做错以下任何事情:

  • 不要并行连接很多。在发送推送通知后重新使用相同的连接或关闭连接。实际上,服务器对最大并行连接数有限制,一旦达到阈值,这可能会给您带来麻烦。此外,Apple 建议保持连接打开,除非您知道它会空闲。

Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave a connection open unless you know it will be idle for an extended period of time—for example, if you only send notifications to your users once a day it is ok to use a new connection each day.

  • 不要向 LIVE APNS 发送开发者配置文件 token 。将分发和开发应用程序 token 分开。如果您尝试将沙盒 token 发送到 LIVE APNS,反之亦然,这可能会导致错误。

关于php - APNS - 通知推送 ios : Connection reset by peer PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38029235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com