標籤:
參考文章:http://blog.csdn.net/showhilllee/article/details/8631734#comments
第一步、下載你工程的開發認證
第二步、從鑰匙串訪問中匯出秘鑰
注意我在這裡使用的密碼是123456
把他們放在同一個檔案裡邊
第三步、認證檔案處理
開啟終端進入上述檔案夾
① 輸入命令把.cer的SSL認證轉換為.pem檔案openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
②輸入命令把私密金鑰test.p12檔案轉化為.pem檔案
openssl pkcs12 -nocerts -out PushChatKey.pem -in test.p12
③對產生的這兩個pem檔案再產生一個pem檔案,來把認證和私密金鑰整合到一個檔案裡:
cat PushChatCert.pem PushChatKey.pem > ck.pem
產生ck.pem檔案
④測試與蘋果推送伺服器串連;
telnet gateway.sandbox.push.apple.com 2195
⑥下面我們要使用我們產生的SSL認證和私密金鑰來設定一個安全的連結去連結蘋果伺服器
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem
輸入Q結束
用戶端我就不說了
第四步、擷取deviceToken及推送php測試代碼
①運行你的App擷取deviceToken,將你擷取到deviceToken填寫至php代碼中的位置;
②在終端輸入php pushMe.php(php代碼稍後貼上)
注意php代碼必須和其他檔案放在一個檔案夾中
php程式碼片段
1 <?php 2 3 // Put your device token here (without spaces): 4 5 $deviceToken = ‘deviceToken去掉中間的空格‘; 6 7 // Put your private key‘s passphrase here:密語 8 $passphrase = ‘123456‘; 9 10 // Put your alert message here:11 $message = ‘這是一條推送訊息‘;12 13 ////////////////////////////////////////////////////////////////////////////////14 15 $ctx = stream_context_create();16 stream_context_set_option($ctx, ‘ssl‘, ‘local_cert‘, ‘ck.pem‘);17 stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $passphrase);18 19 // Open a connection to the APNS server20 $fp = stream_socket_client(21 ‘ssl://gateway.sandbox.push.apple.com:2195‘, $err,22 $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);23 24 if (!$fp)25 exit("Failed to connect: $err $errstr" . PHP_EOL);26 27 echo ‘Connected to APNS‘ . PHP_EOL;28 29 // Create the payload body30 $body[‘aps‘] = array(31 ‘alert‘ => $message,32 ‘sound‘ => ‘default‘33 );34 35 // Encode the payload as JSON36 $payload = json_encode($body);37 38 // Build the binary notification39 $msg = chr(0) . pack(‘n‘, 32) . pack(‘H*‘, $deviceToken) . pack(‘n‘, strlen($payload)) . $payload;40 41 // Send it to the server42 $result = fwrite($fp, $msg, strlen($msg));43 44 if (!$result)45 echo ‘Message not delivered‘ . PHP_EOL;46 else47 echo ‘Message successfully delivered‘ . PHP_EOL;48 49 // Close the connection to the server50 fclose($fp);51 52 ?>
代碼下載:php代碼下載
IOS推送--之開發模式測試