ApnsPHP:推送通知在开发中工作但不在生产中 [英] ApnsPHP: Push notifications working in development but not in production

查看:36
本文介绍了ApnsPHP:推送通知在开发中工作但不在生产中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的:这个问题有很多重复,但没有一个答案有帮助.

我正在关注这个很棒的 Ali Hafizji 关于使用 APNS 服务推送通知的教程.

开发模式中测试 APNS:

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

然后我使用以下命令(使用终端)将两者结合起来:

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

并且(在服务器上使用 ApnsPHP)我可以使用此配置成功发送推送通知:

<代码>...$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,寻找它的正确地址可能是 https://www.entrust.net/downloads/binary/entrust_2048_ca.cer(无论如何它们都是一样的).

在这种情况下,应用程序在调试模式下运行(在设备上,从 XCode 运行)并且一切正常.

生产模式中测试 APNS:

为了在生产模式下测试 APNS,我归档了 AdHoc 分发的应用程序,并使用 iPhone 配置实用程序.

我按照与 aps_production.cer 相同的程序制作了 final_aps_production.pem.

Bang,用于发送推送通知的 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 致命错误:未捕获异常ApnsPHP_Exception",消息无法连接到 'ssl://gateway.push.apple.com:2195': (0)' in/var/www/gettapro/mobile/ApnsPHP/Abstract.php:398\n堆栈跟踪:\n#0/var/www/gettapro/mobile/ApnsPHP/Abstract.php(334): ApnsPHP_Abstract->_connect()\n#1 ....

谷歌搜索(有很多人有这个问题)被证明是徒劳的.

许多不同的建议,例如将持有证书的目录的文件权限更改为 775 甚至如此奇怪......没有任何建议对我有用.

我也在 ApnsPHP/Abstract.php 中尝试过这种更改(此处建议:https://github.com/duccio/ApnsPHP/issues/29)但没有成功.

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

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

当然,我还确保当我测试生产模式时,正确的设备 APNS令牌 - 调试和生产模式下的设备 APNS 令牌不同 - 被使用.

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

尝试通过 telnet 连接 ssl://gateway.push.apple.com:2195 只是为了确保:连接正常.

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

解决方案

aps_production.cer 似乎不应该像 aps_development.cer 那样处理/p>

RTM 时刻.

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

从 Keychain Access 导出 .p12 版本的 aps_production 证书(您也在此处设置密码).

使用此命令将其转换为 .pem 格式(您必须在此处输入密码):

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

然后 - 一切都开始了,我又是一个快乐的露营者了.

Jeremy 有关于如何导出证书和类似教程的很棒的说明.关键在这里.

Yes: there are many duplicates to this question, but none of the answers helped.

I am following this great tutorial by Ali Hafizji on using APNS service for push notifications.

Testing APNS in development mode:

  • download aps_development.cer
  • export the private key for the certificate (aps_development_key.p12)

Then I combined the two using following commands (using 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

And (using ApnsPHP on server) I could successfully send a push-notification with this configuration:

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

A sidenote: i took the entrust_root_certification_authority.pem from https://github.com/jonathanrcarter/push2press, the correct address to look for it would probably be https://www.entrust.net/downloads/binary/entrust_2048_ca.cer (they are the same thing anyway).

In this case application was running in debug mode (on device, run from XCode) and everything was working fine.

Testing APNS in production mode:

To test APNS in production mode i archived the app for AdHoc distribution and installed it on device with iPhone Configuration Utility.

I followed the same procedure with aps_production.cer to make final_aps_production.pem.

Bang, the php script that is called to send push-notifications returned HTML Status Code 500.

The $push generation was of-course modified for production mode:

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

A quick look to /var/log/apache2/error.log indicated the problem:

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

Googling around (there are many people having this problem) proved fruitless.

Many different advices, such even so bizzare as to change the file permissions of the directory holding the certificates to 775... none of the suggestions worked for me.

I have also tried this change in ApnsPHP/Abstract.php (suggested here: https://github.com/duccio/ApnsPHP/issues/29) but with no success.

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

That pesky ApnsPHP_Exception did not go away.

Of course i also made sure that when i was testing the production mode the correct device APNS token - device APNS tokens in debugging and production mode are not the same - was used.

Anyway: tokens can not be a problem since my notification sending script can't even connect to ssl://gateway.push.apple.com:2195.

Tried to connect ssl://gateway.push.apple.com:2195 via telnet just to make sure: connection was fine.

It became obvious: it is a certificate problem.

解决方案

It seems that aps_production.cer shouldn't be handeled the same way as aps_development.cer

Here comes the RTM moment.

Download and install certificate in the keychain (double clicking aps_production.cer)

Export a .p12 version of aps_production certificate (you also set a password here) from the Keychain Access.

Convert it to .pem format using this command (you will have to enter password here):

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

And voilà - everything started working and i am a happy camper again.

There are great tutorial-like instructions by Jeremy on how to export certificate & key here on SO.

这篇关于ApnsPHP:推送通知在开发中工作但不在生产中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆