tp 極光推送

1. 推送類

[php] view plain copy
  1. </pre><pre>  

[php] view plain copy
  1. <?php  
  2. class Jpush{  
  3.     
  4.     private $app_key = 'd349***1184';            //待發送的應用程序(appKey),只能填一個。  
  5.     private $master_secret = 'cd9a****6867aa84f';      //主密碼  
  6.     private $url = "https://api.jpush.cn/v3/push";            //推送的地址  
  7.    
  8.     //若實例化的時候傳入相應的值則按新的相應值進行  
  9.     public function __construct($app_key=null, $master_secret=null,$url=null) {  
  10.         if ($app_key$this->app_key = $app_key;  
  11.         if ($master_secret$this->master_secret = $master_secret;  
  12.         if ($url$this->url = $url;  
  13.     }  
  14.    
  15.     /*  $receiver 接收者的信息 
  16.         all 字符串 該產品下面的所有用戶. 對app_key下的所有用戶推送消息 
  17.         tag(20個)Array標籤組(並集): tag=>array('昆明','北京','曲靖','上海'); 
  18.         tag_and(20個)Array標籤組(交集): tag_and=>array('廣州','女'); 
  19.         alias(1000)Array別名(並集): alias=>array('93d78b73611d886a74*****88497f501','606d05090896228f66ae10d1*****310'); 
  20.         registration_id(1000)註冊ID設備標識(並集): registration_id=>array('20effc071de0b45c1a**********2824746e1ff2001bd80308a467d800bed39e'); 
  21.      
  22.         $content 推送的內容。 
  23.         $extras  附加字段  array類型 
  24.         $m_time 保存離線時間的秒數默認爲一天(可不傳)單位爲秒 
  25.     */  
  26.     public function push($receiver='all'$title=''$content=''$extras$m_time='86400'){  
  27.         $base64=base64_encode("$this->app_key:$this->master_secret");  
  28.         $header=array("Authorization:Basic $base64","Content-Type:application/json");  
  29.         $data = array();  
  30.         $data['platform'] = 'android';          //目標用戶終端手機的平臺類型android,ios,winphone  
  31.         $data['audience'] = $receiver;          //目標用戶  
  32.         //發送通知  
  33.         $data['notification'] = array(  
  34.             //統一的模式--標準模式  
  35.             //"alert"=>$content,     
  36.             //安卓自定義  
  37.             "android"=>array(  
  38.                 "alert"=>$content,  
  39.                 "title"=>$title,  
  40.                 "builder_id"=>1,  
  41.                 "extras"=>$extras  
  42.             ),  
  43.             //ios的自定義  
  44.             "ios"=>array(  
  45.                 // "alert"=>$content,  
  46.                 "badge"=>"1",  
  47.                 "sound"=>"default",  
  48.                 // "extras"=>array("type"=>$m_type, "txt"=>$m_txt)  
  49.             ),  
  50.         );  
  51.    
  52.         //自定義信息  
  53.         $data['message'] = array(  
  54.             "msg_content"=>$content,  
  55.             "extras"=>$extras  
  56.         );  
  57.    
  58.         //附加選項  
  59.         $data['options'] = array(  
  60.             "sendno"=>time(),  
  61.             "time_to_live"=>$m_time,      //保存離線時間的秒數默認爲一天  
  62.             "apns_production"=>1,        //指定 APNS 通知發送環境:0開發環境,1生產環境。  
  63.         );  
  64.         $param = json_encode($data);  
  65.         $res = $this->push_curl($param,$header);  
  66.            
  67.         if($res){       //得到返回值--成功已否後面判斷  
  68.             return $res;  
  69.         }else{          //未得到返回值--返回失敗  
  70.             return false;  
  71.         }  
  72.     }  
  73.    
  74.     //推送的Curl方法  
  75.     public function push_curl($param="",$header="") {  
  76.         if (empty($param)) { return false; }  
  77.         $postUrl = $this->url;  
  78.         $curlPost = $param;  
  79.         $ch = curl_init();                                      //初始化curl  
  80.         curl_setopt($ch, CURLOPT_URL,$postUrl);                 //抓取指定網頁  
  81.         curl_setopt($ch, CURLOPT_HEADER, 0);                    //設置header  
  82.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求結果爲字符串且輸出到屏幕上  
  83.         curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式  
  84.         curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);  
  85.         curl_setopt($ch, CURLOPT_HTTPHEADER,$header);           // 增加 HTTP Header(頭)裏的字段   
  86.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 終止從服務端進行驗證  
  87.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  88.         $data = curl_exec($ch);                                 //運行curl  
  89.         curl_close($ch);  
  90.         return $data;  
  91.     }  
  92. }  
  93.   
  94. ?>  

2.執行推送的方法

[php] view plain copy
  1.       public function pushMess(){  
  2.     vendor('Jpush/Jpush');  
  3.     $pushObj = new Jpush();  
  4.     //組裝需要的參數  
  5.     $receive = 'all';     //全部  
  6.     //$receive = array('tag'=>array('1','2','3'));      //標籤  
  7.     //$receive = array('alias'=>array('111'));    //別名  
  8.     $title = $_POST['title'];  
  9.     $content = ***;  
  10.     $m_time = '86400';        //離線保留時間  
  11.     $extras = array("versionname"=>***, "versioncode"=>***);   //自定義數組  
  12.     //調用推送,並處理  
  13.     $result = $pushObj->push($receive,$title,$content,$extras,$m_time);  
  14.     if($result){  
  15.         $res_arr = json_decode($result, true);  
  16.         if(isset($res_arr['error'])){   //如果返回了error則證明失敗  
  17.             //錯誤信息 錯誤碼  
  18.             $this->error($res_arr['error']['message'].':'.$res_arr['error']['code'],U('Jpush/index'));      
  19.         }else{  
  20.             //處理成功的推送......  
  21.             //可執行一系列對應操作~  
  22.             $this->success('推送成功~');  
  23.         }  
  24.     }else{      //接口調用失敗或無響應  
  25.         $this->error('接口調用失敗或無響應~');  
  26.     }  
  27. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章