ApnsPHP:推送通知工作在开发,但不是在生产

是的:这个问题有很多重复,但没有任何答案有帮助。

我正在使用Ali Hafizji关于使用APNS服务进行推送通知的伟大教程 。

开发模式testingAPNS:

  • 下载aps_development.cer
  • 导出证书的私钥( aps_development_key.p12

然后我使用下面的命令(使用terminal)将两者结合起来:

 openssl x509 -in aps_development.cer -inform der -out aps_development.pem openssl pkcs12 -nocerts -out aps_development_key.pem -in aps_development.p12 cat aps_development.pem aps_development_key.pem > final_aps_development.pem 

和(在服务器上使用ApnsPHP)我可以用这个configuration成功发送推送通知:

 ... $push = new ApnsPHP_Push(ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,'final_aps_development.pem'); $push->setRootCertificationAuthority('entrust_root_certification_authority.pem'); $push->setProviderCertificatePassphrase('mypassword'); ... 

一个旁注:我从https://github.com/jonathanrcarter/push2press entrust_root_certification_authority.pem,正确的地址寻找它可能是https://www.entrust.net/downloads/binary/entrust_2048_ca.cer (无论如何他们是同样的事情)。

在这种情况下,应用程序以debugging模式运行(在设备上,从XCode运行),一切工作正常。

生产模式下testingAPNS:

要在生产模式下testingAPNS,我将AdHoc分发应用程序归档,并使用iPhoneconfiguration实用程序将其安装到设备上。

我使用aps_production.cer来执行相同的过程来final_aps_production.pem

砰,被称为发送推送通知的PHP脚本返回HTML状态码500。

$push代是当然修改生产模式:

 ... $push = new ApnsPHP_Push(ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION,'final_aps_production.pem'); $push->setRootCertificationAuthority('entrust_root_certification_authority.pem'); $push->setProviderCertificatePassphrase('mypassword'); ... 

快速查看/var/log/apache2/error.log指出了这个问题:

 PHP Fatal error: Uncaught exception 'ApnsPHP_Exception' with message 'Unable to connect to 'ssl://gateway.push.apple.com:2195': (0)' in /var/www/gettapro/mobile/ApnsPHP/Abstract.php:398\nStack trace:\n#0 /var/www/gettapro/mobile/ApnsPHP/Abstract.php(334): ApnsPHP_Abstract->_connect()\n#1 .... 

谷歌search(有很多人有这个问题)被certificate是徒劳的。

许多不同的build议,即使如此bizzare以更改目录持有证书的文件权限为775 …没有任何build议为我工作。

我也在ApnsPHP/Abstract.php (这里build议: https : //github.com/duccio/ApnsPHP/issues/29 )尝试过这个改变,但没有成功。

 $streamContext = stream_context_create(array('ssl' => array( //'verify_peer' => isset($this->_sRootCertificationAuthorityFile), 'cafile' => $this->_sRootCertificationAuthorityFile, 'local_cert' => $this->_sProviderCertificateFile ))); 

那个讨厌的ApnsPHP_Exception并没有消失。

当然,我也确信,当我testing生产模式时,正确的设备APNS令牌 – debugging和生产模式下的设备APNS令牌是不一样的 – 被使用。

无论如何:令牌不能成为一个问题,因为我的通知发送脚本甚至不能连接到ssl://gateway.push.apple.com:2195

试图通过telnet连接ssl://gateway.push.apple.com:2195只是为了确保:连接是好的。

很明显:这是一个证书问题。

似乎aps_production.cer不应该像aps_development.cer一样aps_development.cer

RTM时刻到了 。

在钥匙串中下载并安装证书(双击aps_production.cer

从钥匙串访问中导出aps_production证书的.p12版本(您也可以在此设置密码)。

使用此命令将其转换为.pem格式(您将不得不在此input密码):

 openssl pkcs12 -in aps_production.p12 -out final_aps_production.pem -nodes 

,一切都开始了,我又是一个快乐的露营者。

Jeremy有很多关于如何在SO上输出证书和密钥的类似教程的说明。