2013-01-04 236 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