極光推送android
class Jpush extends Model { /* $receiver="registration_id" : [ "4312kjklfds2", "8914afd2", "45fdsa31" ]; $receiver="tag" : [ "深圳", "廣州", "北京" ] $receiver= "tag" : [ "深圳", "廣州" ] $receiver= "tag_and" : [ "女", "會員"] //自定義類型推送類型 $m_type = 'http';//推送附加字段的類型 $m_txt = 'http://www.groex.cn/';//推送附加字段的類型對應的內容(可不填) 多是url,多是一段文字。 $m_time = '86400';//離線保留時間 * * { "platform" : "all" } * * { "platform" : ["android", "ios"] } * 測試成不成功記得看管理平臺上面的統計信息,由於以前把apns_production參數設置成了生產環境,調用接口成功,但是卻沒有用戶,後來改成了開發環境,就成功了。 */ function jiguang_send($receive, $content, $platform, $m_type, $m_txt, $m_time,$m_userid=0) { $appkey = '85e83e2a75936b872dfec5bd'; //AppKey $secret = 'fa6c7f1b5ea2adb74d7eb5da'; //Secret $postUrl = "https://api.jpush.cn/v3/push"; $base64 = base64_encode("$appkey:$secret"); $header = array("Authorization:Basic $base64", "Content-Type:application/json"); $data = array(); $data['platform'] = $platform; //目標用戶終端手機的平臺類型android,ios,winphone $data['audience'] = $receive; //目標用戶 $data['notification'] = array( //統一的模式--標準模式 "alert" => $content, //安卓自定義 "android" => array( "alert" => $content, "title" => "", "builder_id" => 1, "extras" => array("type" => $m_type, "txt" => $m_txt,"userid" => $m_userid) ), //ios的自定義 "ios" => array( "alert" => $content, "badge" => "1", "sound" => "default", "extras" => array("type" => $m_type, "txt" => $m_txt,"userid" => $m_userid) ) ); //蘋果自定義---爲了彈出值方便調測 $data['message'] = array( "msg_content" => $content, "extras" => array("type" => $m_type, "txt" => $m_txt,"userid" => $m_userid) ); //附加選項 $data['options'] = array( "sendno" => time(), "time_to_live" => $m_time, //保存離線時間的秒數默認爲一天 "apns_production" => 1, //布爾類型 指定 APNS 通知發送環境:0開發環境,1生產環境。或者傳遞false和true ); $param = json_encode($data); // $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); $return_data = curl_exec($ch); //運行curl curl_close($ch); return $return_data; } }
/** * -------------------------------------------------------------------------- * 極光推送 * @param data 推送內容 * @param platform 推送平臺 * -------------------------------------------------------------------------- */ private function _pushMes($data,$platform){ //組裝須要的參數 $registration_id = $data['registration_id'];//註冊ID設備標識 $content = $data['content'];//推送內容 $m_type = $data['m_type'];//推送附加字段的類型1爲用戶其餘用戶登錄須要退出 $m_txt = $data['m_txt'];//推送附加字段的類型對應的內容(可不填) 多是url,多是一段文字。 $type = $data['type'];//1單獨推送2所有推送 if($type==1){ $receive['registration_id'] = array($registration_id); $m_time = 86400;//離線保留時間 }else{ // $receive['registration_id'] = array('161a3797c85761e3645');//35966808210190 $receive='all'; $m_time = 86400;//離線保留時間 } $jpush = new Jpush; $result = $jpush->jiguang_send($receive, $content, $platform, $m_type, $m_txt, $m_time) ; if($result){ $res_arr = json_decode($result, true); if(isset($res_arr['error'])){ //若是返回了error則證實失敗 //錯誤信息 錯誤碼ceh $result = array( 'flag' => $res_arr['error']['code'], 'msg' => $res_arr['error']['message'], 'data' => null ); return $result; }else{ //處理成功的推送...... //可執行一系列對應操做~ $result = array( 'flag' => 1, 'msg' => '推送成功', 'data' => $res_arr ); return $result; } }else{ //接口調用失敗或無響應 $result = array( 'flag' => -2, 'msg' => '接口調用失敗或無響應', 'data' => null ); return $result; } }
調用 _pushMes方法ios
$data = array( 'registration_id' => "", 'content' => "《".$live_val['title']."》將於30分鐘後開播,請及時關注!", 'm_type' => '3', 'm_txt' => '直播通知', 'type' => '2' ); $platform = 'all'; //$platform = array('ios','android'); $this->_pushMes($data,$platform);