iPhone SDK 推送通知随机失败

发布于 2024-08-25 02:42:03 字数 1641 浏览 3 评论 0原文

我有一个包含以下内容的 PHP 文件,该文件在开发证书上完美运行,但是当我切换到生产证书时,PHP 错误并给出以下消息,但它只在大约 50% 的时间内执行此操作。另外50%有效。有人知道为什么会发生这种情况吗?

<?php
// masked for security reason 
$deviceToken = 'xxxxxx'; // jq

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__)."/prod.pem");

$number = 5;

$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
  print "Failed to connect $err $errstr\n";
}
else {
  print "Connection OK\n";
  $msg = $_GET['msg'];
    $payload['aps'] = array('alert' => $msg, 'badge' => 1, 'sound' => 'default');
    $payload = json_encode($payload);

  $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);
}
?>

PHP错误:

警告:stream_socket_client()[function.stream-socket-client]:无法设置本地证书链文件“/var/www/vhosts/thissite.com/httpdocs/prod.pem”;检查您的 cafile/capath 设置是否包含您的证书及其颁发者的详细信息,位于 /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php 第 19 行

警告:stream_socket_client() [function.stream-socket-client]:无法在 /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php 第 19 行创建 SSL 句柄

警告:stream_socket_client() [function.stream-socket-client]:无法在 /var/www 中启用加密/vhosts/thissite.com/httpdocs/pushMessageLive.php 第 19 行

警告:stream_socket_client() [function.stream-socket-client]:无法连接到 ssl://gateway.sandbox.push.apple.com:2195 (未知错误)位于 /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php 第 19 行 连接失败 0

I have a PHP file with the following content that works perfectly on development ceritficates, but when I switch to a production certificate the PHP errors and gives the below message, but it only does this about 50% of the time. The other 50% it works. Anyone know why this might be happening?

<?php
// masked for security reason 
$deviceToken = 'xxxxxx'; // jq

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__)."/prod.pem");

$number = 5;

$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
  print "Failed to connect $err $errstr\n";
}
else {
  print "Connection OK\n";
  $msg = $_GET['msg'];
    $payload['aps'] = array('alert' => $msg, 'badge' => 1, 'sound' => 'default');
    $payload = json_encode($payload);

  $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);
}
?>

The PHP error:

Warning: stream_socket_client() [function.stream-socket-client]: Unable to set local cert chain file `/var/www/vhosts/thissite.com/httpdocs/prod.pem'; Check that your cafile/capath settings include details of your certificate and its issuer in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19

Warning: stream_socket_client() [function.stream-socket-client]: failed to create an SSL handle in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19

Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Failed to connect 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤者何惧 2024-09-01 02:42:03

我有同样的问题。您必须与 Apple 的推送通知服务器建立持久的套接字连接。我已经为名为 pyapns 的 python 守护进程编写了教程(http://github .com/samuraisam/pyapns)这对我来说非常有效:

http:// www.how2s.org/index.php/How_to_get_started_with_Apple_Push_Notifications_for_iPhone_or_iPhone_Touch

假设您正在运行 Debian 并且拥有 root 访问权限来安装所需的软件包(例如 python-twisted、libcurl4-openssl-dev 等),此方法可以正常工作。

I had the same issue. You have to make a persistent socket connection with Apple's Push Notification Server. I've written up a tutorial for a python daemon called pyapns (http://github.com/samuraisam/pyapns) which worked very well for me:

http://www.how2s.org/index.php/How_to_get_started_with_Apple_Push_Notifications_for_iPhone_or_iPhone_Touch

This works assuming you are running Debian and have root access to install the required packages such as python-twisted, libcurl4-openssl-dev etc.

清眉祭 2024-09-01 02:42:03

听起来连接太多了。苹果的文档指出,您需要保持连接打开并同时发送尽可能多的数据。重新打开就认为是DOS攻击。因此,请尝试使其持久化,看看是否会出现相同的错误。

Sounds like too many connects. Apple's docs state that you need to hold the connection open and send as many as you can at the same time. Re-opening is considered DOS attack. So try making it persistent and see if you get same error.

番薯 2024-09-01 02:42:03

我不知道您遇到的错误是否是由于与推送服务器的连接过多所致......根据我的经验,这些限制有点难以达到。

但另一方面,当我尝试发送批量推送通知时,PHP 表现得很奇怪。我不确定您的示例代码,但我猜您对每条消息都执行 stream_socket_client()fclose() 吗?在 PHP 中使用 SSL 套接字技术,我个人完成的唯一一件事就是失败...

我不确定您是否有可能在您的服务器上运行 Ruby,但如果可以,我建议切换到 < a href="http://code.google.com/p/ruby-apns-daemon/" rel="nofollow noreferrer" title="ruby-apns-daemon">ruby-apns-daemon 来处理与苹果服务器的对话。它是轻量级的并且易于在 PHP 中实现(您实际上可以编写相同的有效负载 JSON,但将其发送到 ruby​​-apns-daemon 而不是通过套接字)。

I don't know if the error you are experiencing are because of too many connects to the push servers... In my experience, those limits are a bit hard to reach.

But PHP on the other hand have been acting strange when I've tried to send batches of push notifications. I'm not sure from your sample code, but I guess you do a stream_socket_client() and fclose() for every message? Using that technique with SSL sockets in PHP, the only thing I've personally have accomplished is failure...

I'm not sure if you have the possibility to run Ruby on your server, but if you can, I recommend switching to ruby-apns-daemon to handle the talk with Apple's servers. It's lightweight and easy to implement in PHP (you practically compose the same payload-JSON, but send it to ruby-apns-daemon instead of through a socket).

他不在意 2024-09-01 02:42:03

我也遇到过同样的问题,证书有问题。您可以在此处查看解决方案 我该如何做与 PHP 的 SSL 连接 和此处 在 PHP 中使用 ssl 证书时出错

希望它能帮助你。

郑重声明,您没有义务与 APNS 建立持久连接。虽然最好一次发送所有消息,但您可以多次连接和断开连接。我引用Apple的网站:

您还应该保留联系
跨多个 APN
通知。 APN 可能会考虑
快速且可靠的连接
屡建屡拆
作为拒绝服务攻击。之上
错误,APNs 关闭连接
发生错误的地方。

如果您一次不创建数百个连接,则不会遇到麻烦。

I've had the same issue and certificate was in fault. You can see solutions here How can I do an SSL connection with PHP and here Error using ssl cert with PHP.

Hope it'll help you.

And for the record you are not obliged to make a persistent connection with APNS. Though it's best to send all your messages at once, you can connect and disconnect multiple times. I quote Apple's website :

You should also retain connections
with APNs across multiple
notifications. APNs may consider
connections that are rapidly and
repeatedly established and torn down
as a denial-of-service attack. Upon
error, APNs closes the connection on
which the error occurred.

If you don't create hundred of connections at a time you should not get troubles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文