推送通知:我几乎总是收到“连接失败”消息但成功了几次
我正在测试带有推送通知的 iPhone 应用程序。
在过去的 5 天里,我让它工作了几次(它通常适用于几分钟内的连续通知)。
相反,我几乎总是收到错误消息:“连接失败”。
由于它工作了几次,我认为代码是正确的,并且证书也有效。所以我不知道如何解决这个问题。
我还尝试使用以下代码多次连接:
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
for ( $tries = 5, $interval = 10, $fp = false; !$fp && $tries > 0; $tries-- ) {
if (!($fp)) {
print "Failed to connect $err $errstrn";
sleep ( $interval );
}
}
if ($fp) {
...
输出:无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)
谢谢
I'm testing an iPhone app with push notifications.
In the last 5 days I got it working few times (and it usually worked for consecutive notifications in a range of time of few minutes).
Instead I almost always get the error message: "Connection failed".
Since it worked few times, I assume the code to be correct, and the certificates valid as well. So I have no clue how to solve this.
I've also tried to connect multiple times with the following code:
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
for ( $tries = 5, $interval = 10, $fp = false; !$fp && $tries > 0; $tries-- ) {
if (!($fp)) {
print "Failed to connect $err $errstrn";
sleep ( $interval );
}
}
if ($fp) {
...
Output:unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused)
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码看起来大部分是正确的。我建议你不需要循环(我从来没有);如果您过度发送相同的请求,甚至可能会给您带来麻烦。我不确定为什么会有一些成功和一些失败,我发现 APN 服务器非常一致。
需要检查的一个细节:您使用的 PHP 代码的 ssl 选项中不包含密码;如果您使用的 pem 文件受密码保护,则这是必需的。 (例如,请参阅下面的代码)
我建议重新验证您进行身份验证的凭据。执行此操作的最佳方法是使用
openssl
(从终端),如 Apple 的故障排除技术说明 2265 中所述:http://developer.apple.com/library/ios/#technotes/tn2265/_index.html。我已经就以下问题写了一篇很好的演练: 无法连接到 APNS 沙箱服务器验证 pem 文件后,您可以尝试使用以下 PHP 代码(从我的测试中窃取)页):
希望有帮助。
The code looks mostly correct. I would suggest that you don't need the loop (I never have); and it might even cause you trouble if you are over-sending the same requests. I'm not sure why you would have some successes and some failures, I have found APN servers to be pretty consistent.
One detail to check: the PHP code you're using does not include a password in the ssl options; this is required if the pem file you are using is password-protected. (See code below for example)
I would recommend re-validating the credentials you authenticate with. The best way to do this is to use
openssl
(from terminal) as described in Apple's troubleshooting technote 2265: http://developer.apple.com/library/ios/#technotes/tn2265/_index.html. I've written up a good walkthrough on the following SO question: Couldn't able to connect to APNS Sandbox serverAfter validating the pem file, you could try using the following PHP code (stolen from my test page):
Hope that helps.