复制代码 $ 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.easck.com//$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);
?>










