设为首页 加入收藏

TOP

iOS 实现推送消息
2014-11-23 23:19:43 来源: 作者: 【 】 浏览:5
Tags:iOS 实现 推送 消息

iOS消息推送的工作机制可以简单的用下图来概括:





Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务器。


上图可以分为三个阶段:


第一阶段:应用程序把要发送的消息、目的iPhone的标识打包,发给APNS。


第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发送到iPhone。


第三阶段:iPhone把发来的消息传递给相应的应用程序,并且按照设定弹出Push通知。




从上图我们可以看到:


1、应用程序注册消息推送。


2、iOS从APNS Server获取device token,应用程序接收device token。


3、应用程序将device token发送给PUSH服务端程序。


4、服务端程序向APNS服务发送消息。


5、APNS服务将消息发送给iPhone应用程序。


步骤一、


创建需要的证书 & 使用PHP编写服务器端推送代码


1. 登录 iPhone Developer Connection Portal(http://developer.apple.com/iphone/manage/overview/index.action ) 然后点击 App IDs
2. 创建一个 Apple ID ,如: com.tadpole.TestAPNs 注意:通配符 ID 不能用于推送通知服务。
3. 点击Apple ID旁的“Configure”,根据“向导” 的步骤生成一个签名上传,然后下载生成的许可证。
4. 双击.cer文件将你的 aps_development.cer 导入Keychain中。
5. 在Mac上打开“钥匙串访问”,然后在“登录”中选择 "密钥"分类,找到我们创建的证书,然后右击“Apple Development IOS Push Services: com.tadpole.TestAPNs” > 导出 “Apple Development IOS Push Services: com.tadpole.TestAPNs”。保存为 cert.p12 文件。
6. 通过终端命令将这个cert.p12文件转换为PEM格式,打开终端,cd 进入证书所在目录,执行如下命令:


$ openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts


执行完上面命令会在当前目录下生成一个名为apple_push_notification_production.pem文件,这个文件就是我们需要得到php连接APNS 的文件,将apple_push_notification_production.pem和push.php放入同一目录上传到服务器,push.php的代码如下:


< php


// 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格


$deviceToken = '740f4707bebcf74f 9b7c25d4 8e3358945f6aa01da5ddb387462c7eaf 61bb78ad';



// Put your private key's passphrase here:


$passphrase = 'abc123456';



// Put your alert message here:


$message = 'My first push test!';



////////////////////////////////////////////////////////////////////////////////



$ctx = stream_context_create();


stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_push_notification_production.pem');


stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);



// 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' => $message,


'sound' => 'default'


);



// 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);


>


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇iOS 在 Flash 中集成消息推送服务 下一篇Python 解析html文档模块HTMLPaes..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: