這里因為業務需要使用推送功能 uni 里面前端集成了個推 所以選擇了個推來做推送.
個推的官方文檔地址: http://docs.getui.com/getui/server/php/start/
在個推官方沒有找到 composer 包
只能手動的將 sdk 放到了項目中
sdk下載地址:http://www.getui.com/download/docs/getui/server/GETUI_PHP_SDK_4.1.0.0.zip (官方文檔里有建議從官方文檔下載)
這里的用的是tp5框架 laravel yii 和 ci 中需要用到的朋友 可以根據各自的框架調整下 自行加載到這個類調整下配置讀取即可
下載解壓 把它放到 項目根路徑\extend 文件夾內即可
這里 個推的demo 都是 以函數的方式 呈現的 這里我自己將他整理成了一個類 GeTui.php
<?php namespace getui; class GeTui { private $host = 'http://sdk.open.api.igexin.com/apiex.htm'; //測試 private $appkey = ''; private $appid = ''; private $mastersecret = ''; private function init() { // header("Content-Type: text/html; charset=utf-8"); $this->appid = config('getui.appid'); $this->appkey = config('getui.appkey'); $this->mastersecret = config('getui.mastersecret'); $this->host = config('getui.host'); } public function __construct() { $this->init(); $this->__loader(); } private function __loader() { require_once(dirname(__FILE__) . '/' . 'IGt.Push.php'); require_once(dirname(__FILE__) . '/' . 'igetui/IGt.AppMessage.php'); require_once(dirname(__FILE__) . '/' . 'igetui/IGt.TagMessage.php'); require_once(dirname(__FILE__) . '/' . 'igetui/IGt.APNPayload.php'); require_once(dirname(__FILE__) . '/' . 'igetui/template/IGt.BaseTemplate.php'); require_once(dirname(__FILE__) . '/' . 'IGt.Batch.php'); require_once(dirname(__FILE__) . '/' . 'igetui/utils/AppConditions.php'); require_once(dirname(__FILE__) . '/' . 'igetui/template/notify/IGt.Notify.php'); require_once(dirname(__FILE__) . '/' . 'igetui/IGt.MultiMedia.php'); require_once(dirname(__FILE__) . '/' . 'payload/VOIPPayload.php'); } //服務端推送接口,支持三個接口推送 //1.PushMessageToSingle接口:支持對單個用戶進行推送 //2.PushMessageToList接口:支持對多個用戶進行推送,建議為50個用戶 //3.pushMessageToApp接口:對單個應用下的所有用戶進行推送,可根據省份,標簽,機型過濾推送 //單推接口案例 function pushMessageToSingle($cid){ $igt = new \IGeTui($this->host,$this->appkey,$this->mastersecret); //消息模版: // 通知欄消息模板 $template = $this->IGtNotificationTemplateDemo(); // 穿透消息模板 // $template = $this->IGtTransmissionTemplateDemo(); //定義"SingleMessage" $message = new \IGtSingleMessage(); $message->set_isOffline(true);//是否離線 $message->set_offlineExpireTime(3600*12*1000);//離線時間 $message->set_data($template);//設置推送消息類型 //$message->set_PushNetWorkType(0);//設置是否根據WIFI推送消息,2為4G/3G/2G,1為wifi推送,0為不限制推送 //接收方 $target = new \IGtTarget(); $target->set_appId($this->appid); $target->set_clientId($cid); // $target->set_alias(Alias); try { $rep = $igt->pushMessageToSingle($message, $target); var_dump($rep); echo ("<br><br>"); }catch(RequestException $e){ $requstId =e.getRequestId(); //失敗時重發 $rep = $igt->pushMessageToSingle($message, $target,$requstId); var_dump($rep); echo ("<br><br>"); } } //穿透消息模板 public function IGtTransmissionTemplateDemo(){ $template = new \IGtTransmissionTemplate(); $template->set_appId($this->appid); //應用appid $template->set_appkey($this->appkey); //應用appkey //透傳消息類型 $template->set_transmissionType(2); $payload = [ 'title' => '測試', 'content' => '1111', 'payload' => '測試參數' ]; //透傳內容 $template->set_transmissionContent(json_encode($payload)); // $template->set_duration(BEGINTIME,ENDTIME); //設置ANDROID客戶端在此時間區間內展示消息 //這是老方法,新方法參見iOS模板說明(PHP)*/ //$template->set_pushInfo("actionLocKey","badge","message", //"sound","payload","locKey","locArgs","launchImage"); // APN高級推送 // $apn = new \IGtAPNPayload(); // $alertmsg=new \DictionaryAlertMsg(); // $alertmsg->body="body"; // $alertmsg->actionLocKey="ActionLockey"; // $alertmsg->locKey="LocKey"; // $alertmsg->locArgs=array("locargs"); // $alertmsg->launchImage="launchimage"; return $template; } //通知欄消息 (通知欄顯示 點擊啟動應用) function IGtNotificationTemplateDemo(){ $template = new \IGtNotificationTemplate(); $template->set_appId($this->appid); //應用appid $template->set_appkey($this->appkey); //應用appkey $template->set_transmissionType(1);//透傳消息類型 $template->set_transmissionContent("測試離線");//透傳內容 $template->set_title("呵呵");//通知欄標題 $template->set_text("呵呵最新版點擊下載");//通知欄內容 $template->set_logo("http://wwww.igetui.com/logo.png");//通知欄logo $template->set_isRing(true);//是否響鈴 $template->set_isVibrate(true);//是否震動 $template->set_isClearable(true);//通知欄是否可清除 //$template->set_duration(BEGINTIME,ENDTIME); //設置ANDROID客戶端在此時間區間內展示消息 return $template; } //群推接口案例 public function pushMessageToApp(){ $igt = new \IGeTui($this->host,$this->appkey,$this->mastersecret); $template = $this->IGtNotificationTemplateDemo(); //個推信息體 //基於應用消息體 $message = new \IGtAppMessage(); $message->set_isOffline(true); $message->set_offlineExpireTime(10 * 60 * 1000);//離線時間單位為毫秒,例,兩個小時離線為3600*1000*2 $message->set_data($template); // $message->setPushTime("201808011537"); $appIdList=array($this->appid); $phoneTypeList=array('ANDROID'); $provinceList=array('上海'); $tagList=array('中文'); $age = array("0000", "0010"); //推送條件 // $cdt = new \AppConditions(); // 手機類型 // $cdt->addCondition(\AppConditions::PHONE_TYPE, $phoneTypeList); // 地區 // $cdt->addCondition(\AppConditions::REGION, $provinceList); // 標簽 // $cdt->addCondition(\AppConditions::TAG, $tagList); // 年齡? // $cdt->addCondition("age", $age); // $message->set_conditions($cdt); $message->set_appIdList($appIdList); $rep = $igt->pushMessageToApp($message); var_dump($rep); echo ("<br><br>"); } //通知欄顯示 點擊跳轉url function IGtLinkTemplateDemo(){ $template = new \IGtLinkTemplate(); $template ->set_appId($this->appid);//應用appid $template ->set_appkey($this->appkey);//應用appkey $template ->set_title("測試群發消息");//通知欄標題 $template ->set_text("點擊就送66個老鐵666");//通知欄內容 $template ->set_logo("http://wwww.igetui.com/logo.png");//通知欄logo $template ->set_isRing(true);//是否響鈴 $template ->set_isVibrate(true);//是否震動 $template ->set_isClearable(true);//通知欄是否可清除 $template ->set_url("http://www.igetui.com/");//打開連接地址 //$template->set_duration(BEGINTIME,ENDTIME); //設置ANDROID客戶端在此時間區間內展示消息 return $template; } }
將他放到 demo.php 同級目錄
個推的相關配置 我是寫在了
tp5項目\application\extra\getui.php
可以看到我在 GeTui.php 這個類文件里 初始化的時候 進行了讀取 和 加載相關文件
<?php return [ // +--------------------------------- // 個推相關配置 // +--------------------------------- //AppID:由IGetui管理頁面生成,是您的應用與SDK通信的標識之一,每個應用都對應一個唯一的AppID。 'appid' => 'XXX', //AppSecret:第三方客戶端個推集成鑒權碼,用於驗證第三方合法性。在客戶端集成SDK時需要提供。 'AppSecret' => 'XXX', //AppKey:預先分配的第三方應用對應的Key,是您的應用與SDK通信的標識之一。 'appkey' => 'XXX', //MasterSecret:個推服務端API鑒權碼,用於驗證調用方合法性。在調用個推服務端API時需要提供。(請妥善保管,避免通道被盜用)。 'mastersecret' => "XX", // 'host' => "http://sdk.open.api.igexin.com/apiex.htm", ];
將其替換成自己的即可
后面在需要調用的地方使用
//個推案例接口 public function getui() { //實例化之前寫的類 $getui = new \getui\GeTui(); //單發測試 $cid 客戶端id 前端獲取 // $getui->pushMessageToSingle($cid); // 群發測試 $getui->pushMessageToApp(); // dump($getui);die; }
下載個推注冊時 生成的app 即可后端調試
到了這里基本就可以 開心的測試接口了
//這個是我案例中 單發接口調用的模板 群發我這里用的也是這個模板 //相關的其他模板可以在demo 中 找一下 根據自己需求調整 $template = $this->IGtNotificationTemplateDemo();
這里的是生成了一個模板 影響到 消息推送到后的 下一步動作 根據自己的業務需求編輯和選擇即可
這里放其他使用其他模板的案例
//通知欄顯示 點擊跳轉url $template = $this->IGtLinkTemplateDemo()
安卓 推送通知 和 穿透消息都能接收到
ios 只能接收到 穿透消息