iPhone SDK推送通知随机失败

时间:2022-08-26 22:14:02

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文件,其中包含以下内容,可以在开发ceritficates上完美地工作,但是当我切换到生产证书时,会出现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);
}
?>

The PHP error:

PHP错误:

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

警告:stream_socket_client()函数。:无法设置本地cert链文件' /var/www/ dochost / thissite.com/https/prod.pem';请检查您的cafile/capath设置是否包含证书及其颁发者的详细信息,请参见第19行中的/var/ www/vhosts/thissite.com/httpdocs/pushmessagelive.php

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

警告:stream_socket_client()函数。在第19行,未能在/var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php中创建SSL句柄

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

警告:stream_socket_client()函数。在第19行,未能在/var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php中启用加密

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

警告:stream_socket_client()函数。无法连接到ssl://网关.sandbox.push.apple.com:2195(未知错误)在/var/www/ vhosts/thissite.com/httpdocs/pushmessagelive.php在第19行没有连接0。

5 个解决方案

#1


3  

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:

我也有同样的问题。您必须与苹果的Push Notification Server进行持久的套接字连接。我为一个名为pyapns (http://github.com/samuraisam/pyapns)的python守护进程写了一篇教程,对我来说非常有用:

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

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.

如果您正在运行Debian,并且具有安装所需包(如python-twisted、libcurl4- openssldev)等的根访问权,则可以使用这种方法。

#2


1  

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.

听起来有太多的联系。苹果的文档称,你需要打开连接,同时尽可能多地发送。重新打开被认为是DOS攻击。所以试着让它持久,看看你是否得到同样的错误。

#3


0  

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...

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

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

我不确定您是否有可能在服务器上运行Ruby,但如果有可能,我建议您切换到Ruby - aps -守护进程,以处理与苹果服务器的对话。它是轻量级的,并且很容易在PHP中实现(您实际上编写了相同的payload-JSON,但是将它发送给ruby- api -守护进程,而不是通过套接字)。

#4


0  

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.

我有同样的问题,证书有问题。您可以在这里看到解决方案,如何使用PHP进行SSL连接,在这里使用PHP使用SSL cert会出错。

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 :

并且为了记录,您不必与APNS进行持久连接。虽然最好一次发送所有的消息,但是您可以多次连接和断开连接。我引用苹果的网站:

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.

您还应该跨多个通知保留与APNs的连接。APNs可以将快速、反复建立并被破坏的连接视为拒绝服务攻击。当发生错误时,APNs关闭发生错误的连接。

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

如果你不同时建立上百个联系,你就不会有麻烦。

#5


-1  

You should consider using Urban Airship for something like this. We have nice restful API http://urbanairship.com/docs/push_index.html and lots of add-on features and the indie plan is free.

你应该考虑使用城市飞艇。我们有很棒的restful API http://urbanairship.com/docs/push_index.html和许多附加特性,独立计划是免费的。

(I work there)

(我的工作)

#1


3  

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:

我也有同样的问题。您必须与苹果的Push Notification Server进行持久的套接字连接。我为一个名为pyapns (http://github.com/samuraisam/pyapns)的python守护进程写了一篇教程,对我来说非常有用:

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

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.

如果您正在运行Debian,并且具有安装所需包(如python-twisted、libcurl4- openssldev)等的根访问权,则可以使用这种方法。

#2


1  

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.

听起来有太多的联系。苹果的文档称,你需要打开连接,同时尽可能多地发送。重新打开被认为是DOS攻击。所以试着让它持久,看看你是否得到同样的错误。

#3


0  

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...

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

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

我不确定您是否有可能在服务器上运行Ruby,但如果有可能,我建议您切换到Ruby - aps -守护进程,以处理与苹果服务器的对话。它是轻量级的,并且很容易在PHP中实现(您实际上编写了相同的payload-JSON,但是将它发送给ruby- api -守护进程,而不是通过套接字)。

#4


0  

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.

我有同样的问题,证书有问题。您可以在这里看到解决方案,如何使用PHP进行SSL连接,在这里使用PHP使用SSL cert会出错。

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 :

并且为了记录,您不必与APNS进行持久连接。虽然最好一次发送所有的消息,但是您可以多次连接和断开连接。我引用苹果的网站:

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.

您还应该跨多个通知保留与APNs的连接。APNs可以将快速、反复建立并被破坏的连接视为拒绝服务攻击。当发生错误时,APNs关闭发生错误的连接。

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

如果你不同时建立上百个联系,你就不会有麻烦。

#5


-1  

You should consider using Urban Airship for something like this. We have nice restful API http://urbanairship.com/docs/push_index.html and lots of add-on features and the indie plan is free.

你应该考虑使用城市飞艇。我们有很棒的restful API http://urbanairship.com/docs/push_index.html和许多附加特性,独立计划是免费的。

(I work there)

(我的工作)