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

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

问题描述

我和推送通知测试一个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".

由于它的工作几次,我假设code是正确的,且证书有效期为好。所以,我不知道如何解决这个问题。

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.

我也试过多次与下列code连接:

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)

感谢

推荐答案

在code看上去大多是正确的。我建议你​​不需要循环(我从来没有);它甚至可能导致你麻烦,如果你有过发送相同的请求。我不知道为什么你会有一些成功和一些失败,我发现APN服务器是pretty是一致的。

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.

一个细节进行检查:您正在使用不包含在SSL选项密码PHP code;如果您使用的是PEM文件受密码保护这是必需的。 (见下文例如code)

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 (从终端),如苹果公司的故障排除技术说明2265描述:的 http://developer.apple.com/library/ios/#technotes/tn2265/_index.html 。我已经写了以下问题SO一个很好的演练:<一href=\"http://stackoverflow.com/questions/6776864/couldnt-able-to-connect-to-apns-sandbox-server/7082645#7082645\">Couldn't能够连接到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 code(从我的测试页被盗):

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;

希望有所帮助。

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

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