整合極光推送 定時推送
阿新 • • 發佈:2018-12-10
前端,後端,極光推送之間的邏輯關係
(誤區:php與前端app要對接,其實不要直接對接,php直接呼叫極光伺服器就可以了)
一、安裝jpush 極光推送
在composer的配置檔案中加入
"require": {
"jpush/jpush": "^3.5"
}
使用composer 安裝極光推送
cmd 命令
composer install 或者 composer update
1
安裝依賴以後 進入步驟二。
/** * @param $content 推送的內容。 * @param $receiver 接受者資訊 * @param string $m_type 推送附加欄位的型別(可不填) http,tips,chat.... * @param string $m_txt 推送附加欄位的型別對應的內容(可不填) 可能是url,可能是一段文字。 * @return bool|mixed|string */ public static function send_msg($title= "",$content='',$receiver='all',$m_type='',$m_txt='',$m_time){ require '../vendor/jpush/jpush/autoload.php';//引入極光推送擴充套件 這裡的jpush.jpush是你的vendor下極光推送的安裝目錄。 $config = '';//配置你的相關配置包括key secret 離線時間 傳送環境等;注意是個陣列。 $jpush = \Yii::$app->params['jpush']; $app_key = $jpush['jpush_key']; $m_time = $jpush['jpush_time']; $master_secret = $jpush['jpush_secret']; $base64=base64_encode("$app_key:$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"=>$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"=>$jpush['jpush_ambient'], //指定 APNS 通知傳送環境:0開發環境,1生產環境。 ); $param = json_encode($data); $res = self::push_curl($param,$header,$jpush['jpush_url']); if($res){ //得到返回值--成功已否後面判斷 return $res; }else{ //未得到返回值--返回失敗 return false; } } //推送的Curl方法 public static function push_curl($param="",$header="",$postUrl) { if (empty($param)) { return false; } $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; }
配置
//極光配置
'jpush' => [
'jpush_key' => '',
'jpush_secret' => '',
'jpush_ambient' => '0',
'jpush_url' => 'https://api.jpush.cn/v3/push',
],
控制器
public function actionTest(){ $request = \Yii::$app->request; if ($request->isPost) { $attributes = $request->post(); $contnet = isset($attributes['contnet']) ? $attributes['contnet'] : ''; $times = isset($attributes['time']) ? $attributes['time'] : ''; $times = (int)$times; $list =JpushServer::send_msg($contnet,"all","","",$times); return $list; } else { return $this->returnData('', -1, '請求方式錯誤'); } }
前端準備的工作:
在使用者登入時做以下處理
1.獲取應用裝置id,給應用新增別名或標籤(記得一定要有要不然提示1011)
2.將設定的別名傳到極光伺服器
3.將使用者資訊與對應的裝置號,別名,登入狀態等資訊存到公司的伺服器中,以便他用
最後,我用的是定時器任務去觸發任務。不知道你們用的是什麼方法來觸發訊息通知任務。
下面是定時推送介面,按照官方文件走就可以了
呼叫
//定時任務推送 $send_time =date('Y-m-d H:i:s',$times); $res = JpushServer::send_msg_sc($bulletin->title,$bulletin->desc,"all","","",$times,$send_time);
封裝方法
/**
* @param string $title
* @param string $content
* @param string $receiver
* @param string $m_type
* @param string $m_txt
* @param $m_time
* @param $send_time 推送時間
* @return bool|mixed
* 極光定時推送
*/
public static function send_msg_sc($title= "",$content='',$receiver='all',$m_type='',$m_txt='',$m_time,$send_time){
$config = '';//配置你的相關配置包括key secret 離線時間 傳送環境等;注意是個陣列。
$jpush = \Yii::$app->params['jpush'];
$app_key = $jpush['jpush_key'];
$m_time = $jpush['jpush_time'];
$jpush_url_schedules = $jpush['jpush_url_schedules'];
$master_secret = $jpush['jpush_secret'];
$base64=base64_encode("$app_key:$master_secret");
$header=array("Authorization:Basic $base64","Content-Type:application/json");
$data = array();
$data["name"] = $title;
$data["enabled"] = true;
$data["trigger"] = array(
"single"=>array(
"time"=>$send_time
),
);
$data["push"] = array(
"platform" => "all",
"audience" => "all",
"notification"=>array(
"alert"=> $content
),
"message"=>array(
"msg_content"=> $content
),
"options"=>array(
"time_to_live"=> $m_time
),
);
$param = json_encode($data);
$res = self::push_curl($param,$header,$jpush_url_schedules);
if($res){ //得到返回值--成功已否後面判斷
return $res;
}else{ //未得到返回值--返回失敗
return false;
}
}
//推送的Curl方法
public static function push_curl($param="",$header="",$postUrl) {
if (empty($param)) { return false; }
$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;
}