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

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

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