php - 通过 PHP 发送推送通知

标签 php ssl push-notification apple-push-notifications apns-php

我知道这是关于 SO 的一个已经被利用的主题,但我正在为特定的配置而苦苦挣扎。

我正在使用专用服务器向 iOS 设备发送推送通知。从这个服务器我可以通过 telnet 成功连接到 APNS 网关:

[root@..... luca]# telnet gateway.sandbox.push.apple.com 2195
Trying 17.110.227.35...
Connected to gateway.sandbox.push.apple.com.
Escape character is '^]'.

我已经正确生成了 CA 证书以及来自苹果的组合证书和 key ,我可以正确地使用它们测试 OpenSSL 连接:

[root@... luca]#  openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert server_certificates_bundle_sandbox.pem -key server_certificates_bundle_sandbox.pem -CApath entrust_2048_ca.cer 

CONNECTED(00000003)
depth=2 /O=Entrust.net/OU=www.entrust.net/CPS_2048 incorp. by ref. (limits liab.)/OU=(c) 1999 Entrust.net Limited/CN=Entrust.net Certification Authority (2048)
verify return:1
depth=1 /C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C
verify return:1
depth=0 /C=US/ST=California/L=Cupertino/O=Apple Inc./CN=gateway.sandbox.push.apple.com
verify return:1
---
Certificate chain
 0 s:/C=US/ST=California/L=Cupertino/O=Apple Inc./CN=gateway.sandbox.push.apple.com
   i:/C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C
 1 s:/C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C
   i:/O=Entrust.net/OU=www.entrust.net/CPS_2048 incorp. by ref. (limits liab.)/OU=(c) 1999 Entrust.net Limited/CN=Entrust.net Certification Authority (2048)
---
Server certificate
-----BEGIN CERTIFICATE-----
.... hidden cert here ....
-----END CERTIFICATE-----
subject=/C=US/ST=California/L=Cupertino/O=Apple Inc./CN=gateway.sandbox.push.apple.com
issuer=/C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C
---
Acceptable client certificate CA names
/C=US/O=Apple Inc./OU=Apple Certification Authority/CN=Apple Root CA
/C=US/O=Apple Inc./OU=Apple Worldwide Developer Relations/CN=Apple Worldwide Developer Relations Certification Authority
/C=US/O=Apple Inc./OU=Apple Certification Authority/CN=Apple Application Integration Certification Authority
---
SSL handshake has read 3160 bytes and written 2158 bytes
---
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: .... master key here ....
    Key-Arg   : None
    Krb5 Principal: None
    Start Time: 1437564170
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---

我也可以使用 Mac Os X Pusher使用我的开发箱中的证书成功发送推送通知。

我尝试使用他们示例代码的修改版本来使用 ApnsPHP:

<?php
// Adjust to your timezone
date_default_timezone_set('Europe/Rome');
// Report all PHP errors
error_reporting(-1);
// Using Autoload all classes are loaded on-demand
require_once 'ApnsPHP/ApnsPHP/Autoload.php';
// Instantiate a new ApnsPHP_Push object
$push = new ApnsPHP_Push(
        ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
        /*'server_certificates_bundle_sandbox.pem'*/
        'server_certificates_bundle_sandbox.pem'
);
// Set the Provider Certificate passphrase
$push->setRootCertificationAuthority('entrust_2048_ca.cer');
// Connect to the Apple Push Notification Service
$push->connect();
// Instantiate a new Message with a single recipient
$message = new ApnsPHP_Message('5ad1fafb8efdec85fc3e51ea0075d342d18bad9e56cf3e014b56ea9fc4f184bd');
// Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
// over a ApnsPHP_Message object retrieved with the getErrors() message.
$message->setCustomIdentifier("Message-Badge-3");
// Set badge icon to "3"
$message->setBadge(3);
// Set a simple welcome text
$message->setText('Hello APNs-enabled device!');
// Play the default sound
$message->setSound();
// Set a custom property
$message->setCustomProperty('acme2', array('bang', 'whiz'));
// Set another custom property
$message->setCustomProperty('acme3', array('bing', 'bong'));
// Set the expiry value to 30 seconds
$message->setExpiry(30);
// Add the message to the message queue
$push->add($message);
// Send all messages in the message queue
$push->send();
// Disconnect from the Apple Push Notification Service
$push->disconnect();
// Examine the error message container
$aErrorQueue = $push->getErrors();
if (!empty($aErrorQueue)) {
        var_dump($aErrorQueue);
}
?>

从控制台调用时会导致错误:

[root@..... luca]# php index.php 
Wed, 22 Jul 2015 13:30:47 +0200 ApnsPHP[19972]: INFO: Trying tls://gateway.sandbox.push.apple.com:2195...
Wed, 22 Jul 2015 13:30:47 +0200 ApnsPHP[19972]: ERROR: Unable to connect to 'tls://gateway.sandbox.push.apple.com:2195':  (0)
Wed, 22 Jul 2015 13:30:47 +0200 ApnsPHP[19972]: INFO: Retry to connect (1/3)...
Wed, 22 Jul 2015 13:30:48 +0200 ApnsPHP[19972]: INFO: Trying tls://gateway.sandbox.push.apple.com:2195...    
Wed, 22 Jul 2015 13:30:49 +0200 ApnsPHP[19972]: ERROR: Unable to connect to 'tls://gateway.sandbox.push.apple.com:2195':  (0)
Wed, 22 Jul 2015 13:30:49 +0200 ApnsPHP[19972]: INFO: Retry to connect (2/3)...
Wed, 22 Jul 2015 13:30:50 +0200 ApnsPHP[19972]: INFO: Trying tls://gateway.sandbox.push.apple.com:2195...    
Wed, 22 Jul 2015 13:30:50 +0200 ApnsPHP[19972]: ERROR: Unable to connect to 'tls://gateway.sandbox.push.apple.com:2195':  (0)
Wed, 22 Jul 2015 13:30:50 +0200 ApnsPHP[19972]: INFO: Retry to connect (3/3)...
Wed, 22 Jul 2015 13:30:51 +0200 ApnsPHP[19972]: INFO: Trying tls://gateway.sandbox.push.apple.com:2195...   
Wed, 22 Jul 2015 13:30:52 +0200 ApnsPHP[19972]: ERROR: Unable to connect to 'tls://gateway.sandbox.push.apple.com:2195':  (0)

这很奇怪,我尝试了几个小的修复,包括修改 Abstract.php 中的 verify_peer 部分,但都没有用。我在这里遗漏了一些东西,你们知道如何解决这个问题吗?

我已经从 Java 和 Python 发送推送通知有一段时间了,所以我对整个过程很有信心。无论如何,这是我第一次使用 PHP。

最佳答案

我知道这是一篇旧帖子,但我仍然在为 iOS/iPhone 发送推送通知贡献我的评论,因为我曾经遇到过同样的问题:

我能够通过苹果沙箱服务器上的 telnet 进行连接,但无法使用 PHP 套接字脚本进行连接,因为它会出现网关连接错误。请按照以下步骤操作:

  1. 检查 PHP 配置 ( ini ) 以确认 SSL 和套接字 启用。
  2. 没有防火墙相关的限制。
  3. 单独尝试一些核心 PHP 测试通知。你可能会找到一个 here
  4. 现在 openssl 应验证您的 pem 并应确认密码。

注意:如果您已经为您的应用程序生成了 IPA,那么请尝试使用您的生产 pem 而不是开发 pem。

关于php - 通过 PHP 发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31561952/

相关文章:

ssl - Nginx 配置 : if not iPhone/iPad then 301 redirect to https

c# - 证书的 iOS 推送通知 AuthenticationException

android - 通知立即取消

java - 使用 SSLSocketFactory 绕过证书验证

php - 如何使用下面的代码切换类?

php - 列数与第 1 行的值数不匹配

javascript - 单击栏并更改图标颜色时如何激活导航栏

node.js - 没有 SSL,Heroku 上的 Socket.IO 无法工作

ios - iOS中的远程通知和静默通知有什么区别?

php - 有谁知道基于 MVC 框架构建的优秀 PHP CMS?