无法连接到苹果推送通知服务器进行生产


Can't connect to apple push notify server for production

我的推送通知脚本一直在工作,直到我续订过期的证书并上传新的apk。我检查了所有内容,我的证书和配置看起来正确,并且启用了推送通知服务。此外,它还适用于沙盒上的开发证书。

当我使用生产证书时,我遇到了错误,我在互联网上找到的任何内容都没有帮助。这是代码:

<?php
$deviceToken = '1b66005d6ee0e58eac581a29dce21c34083a3560d3a097b8224ce99412c7a5c5';
$message = 'test!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'cafile', 'apple_ca.pem'); // entrust ca certificate
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apn-dist.pem'); // my certificate
// 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;
// sending notification ...

上面的代码转储了此错误:

PHP Warning:  stream_socket_client(): Failed to enable crypto in test.php on line 14
PHP Warning:  stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in test.php on line 14
Failed to connect: 0

当我像这样测试我的证书时:

 openssl s_client -connect gateway.push.apple.com:2195 -cert apn-dist.pem -debug -showcerts -CAfile apple_ca.pem

它是成功的:

New, TLSv1/SSLv3, Cipher is AES256-SHA
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1
    Cipher    : AES256-SHA
    Session-ID:
    Session-ID-ctx:
    Master-Key: 5E9F3C0A551D6A487***********
    Key-Arg   : None
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1458680158
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---

它过去使用以前的证书(也没有ca文件)工作。

我只是通过使用 pem 库来执行所有证书和配置工作而不是手动执行来解决问题。它非常易于使用。

pem -o mycert.pem

输入此命令,将提示您苹果用户名和密码以创建推送通知证书以及其他必要的内容。