JPush 推送消息給 Android

1、註冊開發者賬號:

https://www.jiguang.cn/accounts/register/form

2、註冊好登錄控制檯,創建應用


創建好以後,拿手機掃描二維碼下載:

最後在PHP中運行一下代碼,修改其中的$app_key和$master_secret

$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 'sucess.....';
            return true;
        }
    }else{      //接口調用失敗或無響應
        echo '接口調用失敗或無響應';
        return false;
    }
 
 
class Jpush{
  
    private $app_key = '00c8e702649f75c6a263017e';           //待發送的應用程序(appKey),只能填一個。
    private $master_secret = 'badfdb57256dd342d84d687b';      //主密碼
    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;
    }
}
這時候你的手機上就會收到消息:



發佈了68 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章