2013-01-04 239 views
4

我是一名PHP新手,并在codeigniter中编写推送通知的代码,但我得到了这些错误。在ios中推送通知

这里是我的模型..

function sendmessage($appid, $deviceid, $status, $message) 
{ 

    $deviceToken = '0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78'; 

    $message = 'My first push notification!'; 

    $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
    //stream_context_set_option($ctx, 'ssl', 'passphrase', '[email protected]'); 

    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); 

    if (!$fp){ 
     exit("Failed to connect: $err $errstr" . PHP_EOL); 
       } 
      else { 
      print "Connection OK/n"; 
       } 
    echo 'Connected to APNS' . PHP_EOL; 

    $body['aps'] = array(
     'alert' => $message, 
     'sound' => 'default' 
     ); 
    $payload = json_encode($body); 
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 
    $result = fwrite($fp, $msg, strlen($msg)); 

    if (!$result) 
     echo 'Message not delivered' . PHP_EOL; 
    else 
     echo 'Message successfully delivered' . PHP_EOL; 
    fclose($fp); 

    $data = array(
    'message' => $this->message . 'add', 
    'appid' => $this->appid, 
    'deviceid' => $this->deviceid, 
    'status' => $status 
      ); 
    $this->sendmessage($data); 

错误消息:

Message: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure Message: stream_socket_client(): Failed to enable crypto Message: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error)

+2

你读过这样的回答:http://stackoverflow.com/questions/7453015/ios-push-notification-problem-when-using-crontab-scheduler - 基本上,确保你使用正确的路径到你的'ck.pem'文件。 – Prisoner

+0

确保您的服务器能够到达2195端口上的沙箱服务器 –

回答

1

我一直有同样的问题,你这里提到。这需要花费时间和挠头找到...

,我发现使用下面的代码,纠正握手错误是下载entrust证书,包括这在流方面的解决方案:

$entrustCert = '<full path to cert>/entrust_2048_ca.cer'; 
stream_context_set_option($ctx, 'ssl', 'cafile', entrustCert); 


这似乎与沙箱APN服务间歇性连接问题。我偶尔会返回错误一样:

Warning: stream_socket_client(): SSL: Connection reset by peer 
Warning: stream_socket_client(): Failed to enable crypto 


我希望这是一个人节省时间!

4

一旦您为开发和分发创建了临时证书并推送通知。按照步骤生成推送通知

为了使用您生成的证书,你需要创建一个存储既是PEM文件,您的苹果推送通知服务SSL证书和私钥。您可以从终端创建PEM文件。

导航到包含您之前生成的证书和密钥的目录并执行以下步骤。这里的文件名称反映了作为本课程一部分生成的证书的名称。您必须根据您提供证书的名称更新语法。

首先创建应用程序证书PEM文件。您可以通过双击aps_developer_identity.cer证书文件,然后打开Keychain Assistant并将证书导出为ap12文件,然后将其转换为PEM文件,方式与PushNotificationApp.p12转换为PEM相同文件。或者,您可以使用单个命令行将aps_developer_identity.cer证书文件直接转换为PEM文件。这里我们选择单个命令行选项,如下所示:

openssl x509 -inform der -outform pem -in aps_developer_identity.cer -out PushNotificationAppCertificate.pem 

现在创建应用程序密钥PEM文件,如下所示。您需要输入导入密码和PEM密码短语:

openssl pkcs12 -in PushNotificationApp.p12 -out PushNotificationAppKey.pem -nocerts 

输入导入密码: MAC验证OK 输入PEM密码短语: 验证 - 输入PEM密码短语:

现在将两者连接起来的文件:

cat PushNotificationAppCertificate.pem PushNotificationAppKey.pem > PushNotificationAppCertificateKey.pem 

打开Mac的终端,执行从包含生成的证书的目录下面一行:

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushNotificationAppCertificate.pem -key PushNotificationAppKey.pem 

你再要求您输入您所提交的密钥的密码:

Enter pass phrase for PushNotificationAppKey.pem: 

如果一切正常,那么服务器应该送你很多的,可能看起来像以下信息:

CONNECTED(00000003) 

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 error:num=20:unable to get local issuer certificate verify return:0 
... 
Key-Arg : None 

Start Time: 1326899631 Timeout : 300 (sec) Verify return code: 0 (ok) 
At the end of this, you can enter some text and then select the return key. We entered the text "**Hello World**". 

**Hello World 

closed** 

完成与服务器的通信并验证我们的证书是否正常工作。

enter image description here