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, $i开发者_如何学Cnterval = 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
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 server
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;
Hope that helps.
精彩评论