本文只是記錄一下如何在本身的電腦上配置APNS推送環境,其它的如推送的原理,流程什麼的這裏就不寫了。php
一. 去Apple 開發者中心,建立App ID。注意App ID不能使用通配符。並注意添加Push Notification Servicehtml
對於已經建立的APP ID,也能夠編輯給他添加Push Notification Serviceapache
二. 建立development 和 production的Certificates及Profiles. json
步驟略。安全
注意服務器
1. 建立Profile的時候App ID及Certification要正確。對於已經建立的Profile也能夠再次編輯修改其證書及Devices。修改後只須要到Xcode => References => Accounts中Refresh就能夠了。app
2. 建立證書的時候咱們會用KeyChain先在電腦上建立一個 .certSigningRequest文件,這個文件請保存,由於在證書到期後若是不用這個文件去更新,而用一個新的.certSigningRequest文件,那服務器須要使用的證書就又須要按照如下步驟從新生成。iphone
三. 建立證書給服務器使用socket
1. 在KeyChain中導出對應證書的Private Key。(方便後面使用,記爲Push.p12)ide
2. openssl x509 -in aps_developer_identity.cer -inform der -out PushChatCert.pem
3. openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
4. cat PushChatCert.pem PushChatKey.pem > ck.pem
四. 爲了測試證書是否工做,執行下面的命令:
$ telnet gateway.sandbox.push.apple.com 2195
Trying 17.172.232.226...
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is ‘^]’.
它將嘗試發送一個規則的,不加密的鏈接到 APNS 服務。若是你看到上面的反饋,那說明你的 MAC 可以到
達APNS。按下 Ctrl+C 關閉鏈接。若是獲得一個錯誤信息,那麼你須要確保你的防火牆容許 2195 端口。
而後再次鏈接,此次用咱們的 SSL 證書和私鑰來設置一個安全的鏈接:
$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem
Enter pass phrase for PushChatKey.pem:
你會看到一個完整的輸出,讓你明白 OpenSSL 在後臺作什麼。若是鏈接是成功的,你能夠鍵入一些字符。
當你按下回車後,服務就會斷開鏈接。若是在創建鏈接時有問題,OpenSSL 將會給你一個錯誤消息,
ck.pem 文件就是咱們須要獲得 Push 服務器 鏈接 APNS 的文件。
五. 配置本地服務器
1. 啓用Apache
Mac OS X 10.5.6自帶了Apache 2.2.9,直接在命令行運行apachectl start,Apache就搞定了。
如今Apache的主目錄就是/Libary/WebServer/Documents/,你能夠在這目錄裏放置文件測試了。
2. 啓用PHP
Mac OS X 10.5.6自帶了PHP 5.2.6,咱們須要作的就是將PHP加入Apache中。
修改/etc/apache2/httpd.conf中的 #loadModule php5_module libexec/apache2/libphp5.so 爲
loadModule php5_module libexec/apache2/libphp5.so
而後將/etc/php.ini.default複製爲/etc/php.ini。
cp /etc/php.ini.default /etc/php.ini
以後就能夠按照本身的習慣修改php.ini的配置
好比將error_reporting = E_ALL & ~E_NOTICE 修改成
error_reporting = E_ALL
最後,重啓Apache,能夠在/Libary/WebServer/Documents/目錄中創建個phpinfo.php來測試了。
sudo apachectl restart
3. 將步驟四生成的ck.pem拷貝到/Library/WebServer/Documents/
4. 建立push.php文件,並拷貝到/Libary/WebServer/Documents/
<?php // 這裏是咱們上面獲得的deviceToken,直接複製過來(記得去掉空格) //deviceToken 在測試版本和上線版本上不同。 //lei ipod touch $deviceToken = 'f5b70734ea5f4b91c904544f75457d6ecb908488317e097abaab769e741e6752'; // Put your private key's passphrase here: $passphrase = '1111'; // Put your alert message here: $message = 'My first push test!'; //////////////////////////////////////////////////////////////////////////////// $message = array('msg'=>'小小說閱讀器','title'=>'小小說','url'=>'http://www.apple.com.cn'); //$message = array('msg'=>'去商品詳細頁面','itemtype'=>'2','id'=>'192172'); //$message = array('msg'=>'去菜單頁面','itemtype'=>'1','zktype'=>'1','order'=>'1','zksubtype'=>'1','zktitle'=>'9.9包郵'); //$message = array('msg'=>'軟件升級'); $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); var_dump($ctx); // Open a connection to the APNS server //這個爲正是的發佈地址 //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); //這個是沙盒測試地址,發佈到appstore後記得修改哦 $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL); echo 'Connected to APNS' . PHP_EOL; // Create the payload body $body['aps'] = array( 'alert' => '逗你玩!哈哈。', 'sound' => 'beep.wav', 'badge' => 1 ); $body['type']=2; $body['data']=$message; // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); if (!$result) echo 'Message not delivered' . PHP_EOL; else echo 'Message successfully delivered' . PHP_EOL; // Close the connection to the server fclose($fp); ?>
注意:代碼中的DeviceToken須要在真機上運行起來後,拷貝出來替換。
重啓Apache, sudo apachectl restart
這樣當咱們訪問一次http://localhost/push/push.php就會收到一個通知。
Reference: