2012-01-24 64 views
4

我在本地机器上测试了推送通知,它工作正常。然后我用相同的证书(“.pem”文件)将我的文件上传到真实服务器,并确保端口2195和2196处于打开状态。ios推送通知不能在服务器上工作

我测试:

telnet gateway.sandbox.push.apple.com 2195 

它的工作...

[email protected] ~ # telnet gateway.sandbox.push.apple.com 2195 

Trying 17.149.34.54... 

Connected to gateway.sandbox.push.apple.com. 

Escape character is '^]'. 

但是,当我从我的PHP脚本测试,它返回:

警告:stream_socket_client() [function.stream-socket-client]: 无法连接到ssl://gateway.sandbox.push.apple.com:2195 (Co nnection超时)

警告:stream_socket_client()[function.stream插座-客户]: 无法连接到SSL://feedback.sandbox.push.apple.com:2196

不限建议?


这是PHP代码:

$ctx = stream_context_create(); 

stream_context_set_option($ctx, 'ssl', 'local_cert', 
    "path/to/certificate"); 

$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", 
    $error, $errorString, 100, 
    (STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT), $ctx); 
+0

请显示您的整个PHP代码... – powtac

+0

$ ctx = stream_context_create(); \t \t stream_context_set_option($ ctx,'ssl','local_cert',“path/to/certificate”); $ fp = stream_socket_client(“path/to/certificate”,$ error,$ errorString,100,(STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT),$ ctx); – yasserislam

+0

最好把这个添加到你的问题中,只需编辑它。 –

回答

0

stream_socket_client功能,不应传递证书路径,但APNS URL。

该方法打开的连接并返回它:

public function connect(){ 

     $streamContext = stream_context_create(); 
     stream_context_set_option($streamContext, 'ssl', 'local_cert', '/your/cert/path'); 
     $apns = stream_socket_client('use.apns.url.here', $error, $errorString, 9, STREAM_CLIENT_CONNECT, $streamContext); 

     if (!$apns){ 
      $this->logger->error("Failed to connect to APNS: {$error} {$errorString}."); 
     } 

     return $apns; 
    } 

一旦你建立你的APNS的消息,您可以使用以下

fwrite($apns, $apnsMessage); 

推它,如果你的目标,你可以按照本http://goo.gl/9Q0u是实现你自己的推送通知API。

还有一些现有的PHP库:

和一个非常优秀的Java库(我目前使用它的大规模推):

+0

中添加了正确的php代码谢谢,我编辑了代码,结果是一样的 – yasserislam

+0

您可以尝试没有STREAM_CLIENT_PERSISTENT – pgratton

+0

相同的结果... – yasserislam

2

我有同样的问题接收'无法连接...(连接超时)'。我可以从我的家用机器连接,但我无法从托管服务器上连接。

就我而言,端口2195和2196不是简单地从托管服务器打开。我不得不联系技术支持来打开这些端口。 Ping可能工作,因为它使用不同的端口号。所以请联系您的托管服务公司以确保这些端口是开放的。

祝你好运,

卡兹

5

此代码将正常工作:

$device = 'sdfsdfsfsdffsd'; 
$payload['aps'] = array('alert' => 'Hello I am testing the server code ....', 'badge' => 1, 'sound' => 'default'); 
$payload = json_encode($payload); 

$options = array('ssl' => array(
    'local_cert' =>'ck.pem', 
    'passphrase' => 'abc123' 
)); 



$streamContext = stream_context_create(); 
stream_context_set_option($streamContext, $options); 
$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext); 

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($payload)) . $payload; 
fwrite($apns, $apnsMessage); 
fclose($apns); 

只检查PEM文件路径和端口上。

0

您必须设置防火墙以允许所有17.0.0.0/8块(它们全部属于Apple!)。检查THIS ANSWER

而且根据Apple

的APN的服务器使用的负载均衡,使您的设备无法连接到同一个公网IP地址通知。最好允许访问分配给Apple的整个17.0.0.0/8地址块上的这些端口。

如果您正在使用CSF防火墙(比如我),我建议你添加此行csf.allow文件:

tcp|out|d=2195|d=17.0.0.0/8

使用这个,而不是仅仅“17.0.0.0/ 8“将只允许与苹果公司的连接,特别是端口2195. NSA不会喜欢它,但这样更加精确和安全! ;)