为什么我无法连接到苹果推送通知服务?
我在开发苹果推送通知服务时遇到了一个大问题。
我编写了一个 PHP 脚本(apns.php),它将使用 PEM(ck.pem) 文件作为安全证书,并在我向 APNS 发布 HTTP 请求时建立到 APNS 的 SSL 连接,但它总是尝试连接服务端口(ssl://gateway.sandbox.push.apple.com:2195)
时失败。 PHP 脚本部署在 GoDaddy 的 Linux 主机上(它应该支持 SSL)。
我已经注册成为iOS开发者计划的成员,并且我已经为我的应用程序注册了苹果推送通知服务。我已经从 Keychain Access 生成了一个证书文件和一个密钥文件(cert.p12 和 key.p12)
并将它们转换为 PEM 文件(cert.pem 和 key.pem)
code> 将它们加入到一个(ck.pem)中并将其放在 PHP 脚本的同一目录中。
我想知道我是否做错了什么!您可以参考一下您可能需要的文件的附件吗?
非常感谢!
HTTP 请求如下所示。
http://www.insidertracker.net/apns/apns.php?message=&badge=2&sound=received5.caf
我的请求的回复消息如下:
警告:stream_socket_client() [function.stream-socket-client]: 无法连接到 ssl://gateway.sandbox.push.apple.com:2195 (连接被拒绝)在 /home/content/40/6967940/html/apns/apns.php 上 第 25 行连接失败 111 连接被拒绝
PHP 脚本:
<?php
$deviceToken = '0535dda1 6fd04e87 ed0a8194 d418a6c1 99eec462 8a871891 d062018d c6af4f99';
$pass = 'Php1234'; // Passphrase for the private key (ck.pem file)
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'You have an important message from InsiderTracker';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
// connect to apns
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
// $fp = fsockopen('ssl://gateway.sandbox.push.apple.com:2195', 2195, $err, $errstr, 30);
if (!$fp) {
print "Failed to connect $err $errstr\n";
return;
}
else {
print "Connection OK\n<br/>";
}
// send message
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack ("n",strlen($payload)) . $payload;
print "Sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>
I have meet a big problem when I was doing developing about Apple Push Notification Service.
I wrote a PHP script(apns.php) which will use a PEM(ck.pem) file as security certificate and build a SSL connection to APNS when I post a HTTP request to it, but it always failed when it tried to connect the service port ( ssl://gateway.sandbox.push.apple.com:2195)
. The PHP script is deployed on a Linux Hosting of GoDaddy (it should support SSL).
I have already registered to be a member of iOS developer program,and I have registered the apple push notification service for my application. And I have generated a certificate file and a key file (cert.p12 and key.p12)
from Keychain Access and translate them into PEM files(cert.pem and key.pem)
and join them into one(ck.pem) and put it in the same directory of PHP script .
I want to know if there is something wrong I did ! Would you refer to the attachment of the files that you may need?
Thanks very much!
The HTTP request is something like below.
http://www.insidertracker.net/apns/apns.php?message=&badge=2&sound=received5.caf
The response message of my request is below:
Warning: stream_socket_client() [function.stream-socket-client]:
unable to connect to ssl://gateway.sandbox.push.apple.com:2195
(Connection refused) in /home/content/40/6967940/html/apns/apns.php on
line 25 Failed to connect 111 Connection refused
The PHP script:
<?php
$deviceToken = '0535dda1 6fd04e87 ed0a8194 d418a6c1 99eec462 8a871891 d062018d c6af4f99';
$pass = 'Php1234'; // Passphrase for the private key (ck.pem file)
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'You have an important message from InsiderTracker';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];
// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
/* End of Configurable Items */
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
// assume the private key passphase was removed.
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
// connect to apns
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
// $fp = fsockopen('ssl://gateway.sandbox.push.apple.com:2195', 2195, $err, $errstr, 30);
if (!$fp) {
print "Failed to connect $err $errstr\n";
return;
}
else {
print "Connection OK\n<br/>";
}
// send message
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack ("n",strlen($payload)) . $payload;
print "Sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,GoDaddy 禁止在外来端口(除了 80 或其他一些端口之外的任何端口)上进行传出连接。您可能无法尝试在 GoDaddy 上推出自己的推送服务。我很幸运能用 bluehost 做你正在尝试的事情。
对于使用 PHP 开发推送服务的一般建议,这两篇文章非常宝贵:一个 两个
It's my understanding that GoDaddy prohibits outgoing connections on exotic ports (anything other than 80 a perhaps a few others). You're probably out of luck trying to roll your own push service hosted on GoDaddy. I have had luck doing what you're attempting with bluehost.
For general advice on developing a push service in PHP, these two articles were invaluable: One Two
我也遇到这个错误。您必须记住以下事项:
ssl://gateway.sandbox.push.apple.com
,请检查您的 php 版本支持的 ssl;如果使用tls://gateway.sandbox.push.apple.com<,请检查 tls /code>
您还可以使用 api get 参考来发送推送 开发服务器:
api.development.push.apple.com:443
生产服务器:api.push.apple.com:443
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1
I also got this error. Following things you have to remember:
ssl://gateway.sandbox.push.apple.com
and tls if usingtls://gateway.sandbox.push.apple.com
You can also use api get reference for sending push Development server:
api.development.push.apple.com:443
Production server:api.push.apple.com:443
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1