1. 推送類
- </pre><pre>
- <?php
- class Jpush{
- private $app_key = 'd349***1184'; //待發送的應用程序(appKey),只能填一個。
- private $master_secret = 'cd9a****6867aa84f'; //主密碼
- private $url = "https://api.jpush.cn/v3/push"; //推送的地址
- //若實例化的時候傳入相應的值則按新的相應值進行
- public function __construct($app_key=null, $master_secret=null,$url=null) {
- if ($app_key) $this->app_key = $app_key;
- if ($master_secret) $this->master_secret = $master_secret;
- if ($url) $this->url = $url;
- }
- /* $receiver 接收者的信息
- all 字符串 該產品下面的所有用戶. 對app_key下的所有用戶推送消息
- tag(20個)Array標籤組(並集): tag=>array('昆明','北京','曲靖','上海');
- tag_and(20個)Array標籤組(交集): tag_and=>array('廣州','女');
- alias(1000)Array別名(並集): alias=>array('93d78b73611d886a74*****88497f501','606d05090896228f66ae10d1*****310');
- registration_id(1000)註冊ID設備標識(並集): registration_id=>array('20effc071de0b45c1a**********2824746e1ff2001bd80308a467d800bed39e');
- $content 推送的內容。
- $extras 附加字段 array類型
- $m_time 保存離線時間的秒數默認爲一天(可不傳)單位爲秒
- */
- public function push($receiver='all', $title='', $content='', $extras, $m_time='86400'){
- $base64=base64_encode("$this->app_key:$this->master_secret");
- $header=array("Authorization:Basic $base64","Content-Type:application/json");
- $data = array();
- $data['platform'] = 'android'; //目標用戶終端手機的平臺類型android,ios,winphone
- $data['audience'] = $receiver; //目標用戶
- //發送通知
- $data['notification'] = array(
- //統一的模式--標準模式
- //"alert"=>$content,
- //安卓自定義
- "android"=>array(
- "alert"=>$content,
- "title"=>$title,
- "builder_id"=>1,
- "extras"=>$extras
- ),
- //ios的自定義
- "ios"=>array(
- // "alert"=>$content,
- "badge"=>"1",
- "sound"=>"default",
- // "extras"=>array("type"=>$m_type, "txt"=>$m_txt)
- ),
- );
- //自定義信息
- $data['message'] = array(
- "msg_content"=>$content,
- "extras"=>$extras
- );
- //附加選項
- $data['options'] = array(
- "sendno"=>time(),
- "time_to_live"=>$m_time, //保存離線時間的秒數默認爲一天
- "apns_production"=>1, //指定 APNS 通知發送環境:0開發環境,1生產環境。
- );
- $param = json_encode($data);
- $res = $this->push_curl($param,$header);
- if($res){ //得到返回值--成功已否後面判斷
- return $res;
- }else{ //未得到返回值--返回失敗
- return false;
- }
- }
- //推送的Curl方法
- public function push_curl($param="",$header="") {
- if (empty($param)) { return false; }
- $postUrl = $this->url;
- $curlPost = $param;
- $ch = curl_init(); //初始化curl
- curl_setopt($ch, CURLOPT_URL,$postUrl); //抓取指定網頁
- curl_setopt($ch, CURLOPT_HEADER, 0); //設置header
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求結果爲字符串且輸出到屏幕上
- curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
- curl_setopt($ch, CURLOPT_HTTPHEADER,$header); // 增加 HTTP Header(頭)裏的字段
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 終止從服務端進行驗證
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- $data = curl_exec($ch); //運行curl
- curl_close($ch);
- return $data;
- }
- }
- ?>
2.執行推送的方法
- public function pushMess(){
- vendor('Jpush/Jpush');
- $pushObj = new Jpush();
- //組裝需要的參數
- $receive = 'all'; //全部
- //$receive = array('tag'=>array('1','2','3')); //標籤
- //$receive = array('alias'=>array('111')); //別名
- $title = $_POST['title'];
- $content = ***;
- $m_time = '86400'; //離線保留時間
- $extras = array("versionname"=>***, "versioncode"=>***); //自定義數組
- //調用推送,並處理
- $result = $pushObj->push($receive,$title,$content,$extras,$m_time);
- if($result){
- $res_arr = json_decode($result, true);
- if(isset($res_arr['error'])){ //如果返回了error則證明失敗
- //錯誤信息 錯誤碼
- $this->error($res_arr['error']['message'].':'.$res_arr['error']['code'],U('Jpush/index'));
- }else{
- //處理成功的推送......
- //可執行一系列對應操作~
- $this->success('推送成功~');
- }
- }else{ //接口調用失敗或無響應
- $this->error('接口調用失敗或無響應~');
- }
- }