手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

苹果推送注意事项

首页 > Flutter >

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

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

« 上一篇 | 下一篇 »

只显示10条记录相关文章

golang将byte的int转换 (浏览: 46939, 评论: 1)
javascipt的{}中逗号带来的困扰 (浏览: 30621, 评论: 6)
Yii Behavior的简单用法 (浏览: 20691, 评论: 1)
typecho 插件开发(三) (浏览: 19320, 评论: 0)
Yii的AR效率释疑 (浏览: 18999, 评论: 1)
MySQL Proxy 0.7.0 is finally released (浏览: 18185, 评论: 0)
全文索引的苦逼记事一 (浏览: 16616, 评论: 0)
记录Xcode4.3无法安装的问题 (浏览: 14817, 评论: 0)
买了几本书 (浏览: 14731, 评论: 0)
用ST2写go程序时遇到的坑 (浏览: 14709, 评论: 0)

发表评论

评论内容 (必填):