本文只是记录一下如何在本身的电脑上配置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: