手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:badge

苹果推送注意事项

关于苹果的推送服务,网上的相关资料非常多,最简单的就是组合成一个数组,类似:

XML/HTML代码
  1. $arr['aps'] = array(  
  2.     'badge'=>1,  
  3.     'sound'=>'',  
  4.     'alert'=>'xxx'      
  5. );  

然后用json_encode处理一下之后,用ssl的方式发送给苹果:

XML/HTML代码
  1. chr(0) . pack("n", 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload;  

$devideToken是设备的token,$payload就是上述的数组json_encode之后的数据。
测试的话是发给:ssl://gateway.sandbox.push.apple.com:2195,正式的话,将sandbox去掉即OK

于是这代码就好写了:

PHP代码
  1. /** 
  2.  * ApnsService.php 
  3.  * 
  4.  * @category 
  5.  * @package 
  6.  * @author   gouki <gouki.xiao@gmail.com> 
  7.  * @version 1.0 
  8.  * @created 2011-10-12-23:45 
  9.  */  
  10. class ApnsService {  
  11.     public $token;  
  12.     public $message;  
  13.     public $badge = 1;  
  14.     public $sound;  
  15.     public $ispad;  
  16.     public function __construct($token$message$badge = null, $sound = null, $ispad = 0) {  
  17.         $this->token = $token;  
  18.         $this->message = $message;  
  19.         $this->badge = $badge;  
  20.         $this->sound = $sound;  
  21.         $this->ispad = (int)$ispad;  
  22.     }  
  23.     public function send() {  
  24.         return $this->sendPushInfo();  
  25.     }  
  26.     public function sandboxSend() {  
  27.         return $this->sendPushInfo(true);  
  28.     }  
  29.     private function getPayload() {  
  30.         $body = array();  
  31.         if ($this->badge) {  
  32.             $body['aps']['badge'] = $this->badge;  
  33.         }  
  34.         if ($this->sound) {  
  35.             $body['aps']['sound'] = $this->sound;  
  36.         }  
  37.         $body['aps']['alert'] = $this->message;  
  38.         return $body;  
  39.     }  
  40.     private function getCertFile() {  
  41.         return Yii::getPathOfAlias("application") . ($this->ispad ? "/hdDis.pem" : "/iphoneDis.pem");  
  42.     }  
  43.     private function getSandboxCertFile() {  
  44.         return Yii::getPathOfAlias("application") . ($this->ispad ? "/hdDev.pem" : "/iphoneDev.pem");  
  45.     }  
  46.     private function getApplePushUrl($isSandbox = false) {  
  47.         return ($isSandbox == true ? "ssl://gateway.push.apple.com:2195" : "ssl://gateway.sandbox.push.apple.com:2195");  
  48.     }  
  49.     private function sendPushInfo($isSandbox = false) {  
  50.         $ctx = stream_context_create();  
  51.         stream_context_set_option($ctx'ssl''local_cert',  
  52.                                     ($isSandbox == true  
  53.                                             ? $this->getSandboxCertFile()  
  54.                                             : $this->getCertFile())  
  55.         );  
  56.         //stream_context_set_option($ctx, 'ssl', 'passphrase', '123456'); //如果设置了密码,这里就不能注释了  
  57.         $fp = stream_socket_client($this->getApplePushUrl($isSandbox), $err$errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
  58.         if (!$fp) {  
  59.             print "Failed to connect $err $errstr\n";  
  60.             return false;  
  61.         } else {  
  62.             //print "Connection OK\n";  
  63.         }  
  64.         $payload = json_encode($this->getPayload());  
  65.         //echo strlen($payload); //这里可以精心测试,最大不能超过256个字节即strlen超过256后苹果直接不予处理。  
  66.         $msg = chr(0) . pack("n", 32) . pack('H*'str_replace(' '''$this->token)) . pack("n"strlen($payload)) . $payload;  
  67.         fwrite($fp$msg);  
  68.         fclose($fp);  
  69.         return true;  
  70.     }  
  71. }  

上面的代码非常简单只是作了一个简单的处理和封装。不过有部分路径是基于yii的,所以要改一下就OK了。
主要是自己的记录。

Tags: 推送, badge, json