極光推送Jpush(v3)服務端PHP版本的api腳本類


原文地址:http://www.dodobook.net/php/780

 

關於極光推送的上一篇文章已經說明了,此處就不多說了。使用v3版本的原因是v2使用到2014年年底就停止了。點擊查看上一篇的地址 http://www.dodobook.net/php/777 歡迎移步瀏覽。

昨天才開始拿到極光推送的SDK下載文檔。下載地址 http://docs.jpush.cn/display/dev/Server-SDKs 看了半天也看出眉目。安裝example的方法試了,各種報錯。還需要vendor/autoload.php composer的支持。

后來查找各種方法能實現了。但是感覺好龐大的一個包。且想封裝成為一個類,幾次嘗試都失敗。想想v2版本一個文件實現多好啊。

自己太笨了,網上找找到相關的文檔。自己稍加改動,終於實現,一個類就在后端調用了。直接上代碼:

   

//極光推送的類
//文檔見:http://docs.jpush.cn/display/dev/Push-API-v3
 
/***使用示例
    $pushObj = new Jpush();
    //組裝需要的參數
    //$receive = 'all';     //全部
    //$receive = array('tag'=>array('2401','2588','9527'));      //標簽
    $receive = array('alias'=>array('93d78b73611d886a74*****88497f501'));    //別名
    $content = '這是一個測試的推送數據....測試....Hello World...';
    $m_type = 'http';
    $m_txt = 'http://www.iqujing.com/';
    $m_time = '600';        //離線保留時間
 
    //調用推送,並處理
    $result = $pushObj->push($receive,$content,$m_type,$m_txt,$m_time);
    if($result){
        $res_arr = json_decode($result, true);
        if(isset($res_arr['error'])){                       //如果返回了error則證明失敗
            echo $res_arr['error']['message'];          //錯誤信息
            echo $res_arr['error']['code'];             //錯誤碼
            return false;       
        }else{
            //處理成功的推送......
            echo '推送成功.....';
            return true;
        }
    }else{      //接口調用失敗或無響應
        echo '接口調用失敗或無響應';
        return false;
    }
***/
 
class Jpush{
 
    private $app_key = 'd7fd***********c3642fc';            //待發送的應用程序(appKey),只能填一個。
    private $master_secret = 'a04**********4a80377';        //主密碼
    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 推送的內容。
    //$m_type 推送附加字段的類型(可不填) http,tips,chat....
    //$m_txt 推送附加字段的類型對應的內容(可不填) 可能是url,可能是一段文字。
    //$m_time 保存離線時間的秒數默認為一天(可不傳)單位為秒
    public function push($receiver='all',$content='',$m_type='',$m_txt='',$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'] = 'all';          //目標用戶終端手機的平台類型android,ios,winphone
        $data['audience'] = $receiver;      //目標用戶
         
        $data['notification'] = array(
            //統一的模式--標准模式
            "alert"=>$content,   
            //安卓自定義
            "android"=>array(
                "alert"=>$content,
                "title"=>"",
                "builder_id"=>1,
                "extras"=>array("type"=>$m_type, "txt"=>$m_txt)
            ),
            //ios的自定義
            "ios"=>array(
                // "alert"=>$content,
                "badge"=>"1",
                "sound"=>"default",
                // "extras"=>array("type"=>$m_type, "txt"=>$m_txt)
            ),
        );
 
               //蘋果自定義---為了彈出值方便調測
        $data['message'] = array(
            "msg_content"=>$content,
            "extras"=>array("type"=>$m_type, "txt"=>$m_txt)
        );
 
        //附加選項
        $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;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM