推送通知:我几乎总是收到“连接失败";但它工作了几次 [英] Push Notifications: I almost always get "Connection failed" but it worked few times

查看:17
本文介绍了推送通知:我几乎总是收到“连接失败";但它工作了几次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试带有推送通知的 iPhone 应用.

I'm testing an iPhone app with push notifications.

在过去的 5 天里,我让它工作了几次(通常在几分钟的时间内连续通知工作).

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) {

            ...

输出:无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)

Output:unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused)

谢谢

推荐答案

代码看起来基本正确.我建议你不需要循环(我从来没有);如果您过度发送相同的请求,它甚至可能会给您带来麻烦.我不知道为什么你会有一些成功和一些失败,我发现 APN 服务器非常一致.

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.

要检查的一个细节:您使用的 PHP 代码在 ssl 选项中不包含密码;如果您使用的 pem 文件受密码保护,则这是必需的.(例如参见下面的代码)

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)

我建议您重新验证您使用的凭据.最好的方法是使用 openssl(来自终端),如 Apple 的故障排除技术说明 2265 中所述:http://developer.apple.com/library/ios/#technotes/tn2265/_index.html.我已经就以下 SO 问题编写了一个很好的演练:无法连接到 APNS 沙盒服务器

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 server

验证 pem 文件后,您可以尝试使用以下 PHP 代码(从我的测试页面中窃取):

After validating the pem file, you could try using the following PHP code (stolen from my test page):

// Put your private key's passphrase here:
$passphrase = 'p-a-s-s-p-h-r-a-s-e';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'CertificateAndPrivateKey.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);

echo 'Connected to APNS' . PHP_EOL;

希望有所帮助.

这篇关于推送通知:我几乎总是收到“连接失败";但它工作了几次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆