通过PHP发送推送通知

时间:2015-07-22 11:34:50

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

我知道这是一个已被利用的主题,但我正在努力应对特定的配置。

我正在使用专用服务器向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证书以及来自apple的组合证书和密钥,我可以正确使用它们测试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。

1 个答案:

答案 0 :(得分:0)

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

我能够通过telnet通过苹果沙盒服务器连接,但不能使用PHP套接字脚本,因为它给出了网关连接错误。请按照以下步骤操作:

  1. 检查PHP配置(ini)以确认SSL和套接字 启用。
  2. 没有与防火墙相关的限制。
  3. 分别尝试一些核心PHP测试通知。您可能会找到一个here
  4. 现在openssl应该验证你的pem和密码应该被确认。
  5. 注意:如果您已经为您的APP生成了IPA,那么请尝试使用您的生产工具,而不是开发人员。