uniapp连接php,thinkphp5 对接手机uni-app的unipush推送(个推)
PHP對接unipush(推送)也是要按照個推的文檔進(jìn)行對接(其實unipush用的服務(wù)就是個推的,前者應(yīng)該是和個推談了合作,個推的廠商推送服務(wù)免費開放給uni的開發(fā)人員)
放入項目的extend文件夾(第三方擴展),并在getui的demo文件同級創(chuàng)建一個GeTui.php文件,用來實例化并調(diào)用,class代碼貼在圖片后面
因為需求問題,我項目只有用到單用戶的推送,所以這邊只貼我測試并通過的代碼,沒有多推的示例
這里需要注意的是:消息內(nèi)容決定了系統(tǒng)的通知欄的通知等級,如果通知欄一直沒有消息,可以去查一下各自手機廠商的消息等級,對應(yīng)修改文字
class GeTui
{
private $host = 'http://sdk.open.api.igexin.com/apiex.htm';
//測試
private $appkey = '';
private $appid = '';
private $mastersecret = '';
private function init($appid=null,$appkey=null,$mastersecret=null)
{
$this->appid = !empty($appid) ? $appid : "fdVf****aUq4";
$this->appkey = !empty($appkey) ? $appkey : "fdVf****RUq4";
$this->mastersecret = !empty($mastersecret) ? $mastersecret : "39pj****VtQ5";
$this->host = "http://sdk.open.api.igexin.com/apiex.htm";
}
public function __construct($appid=null,$appkey=null,$mastersecret=null)
{
$this->init($appid,$appkey,$mastersecret);
$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');
}
//單推
function pushMessageToSingle($cid){
$igt = new \IGeTui($this->host,$this->appkey,$this->mastersecret);
// 透傳消息模板
$template = $this->IGtTransmissionTemplateDemo();
//定義"SingleMessage"
$message = new \IGtSingleMessage();
$message->set_isOffline(false);//是否離線
$message->set_offlineExpireTime(3600*12*1000);//離線時間
$message->set_data($template);//設(shè)置推送消息類型
$message->set_PushNetWorkType(0);//設(shè)置是否根據(jù)WIFI推送消息,2為4G/3G/2G,1為wifi推送,0為不限制推送
//接收方
$target = new \IGtTarget();
$target->set_appId($this->appid);
$target->set_clientId($cid);
try {
$rep = $igt->pushMessageToSingle($message, $target);
var_dump($rep);
echo ("
");
}catch(RequestException $e){
$requstId =e.getRequestId();
//失敗時重發(fā)
$rep = $igt->pushMessageToSingle($message, $target,$requstId);
var_dump($rep);
echo ("
");
}
}
//透傳消息模板
public function IGtTransmissionTemplateDemo(){
$listId = [
'title' => '通知',
'content' => '你有一條新消息',
'payload' => [
"push"=> "inner",
"event"=> "warning",
"silent"=> false,
]
];
$mes = [
'title' => '通知',
'content' => '你有一條新消息',
'payload' => [
"push"=> "inner",
"event"=> "warning",
"silent"=> false,
"data"=> ""
]
];
$template = new \IGtTransmissionTemplate();
$template->set_appId($this -> appid);//應(yīng)用appid
$template->set_appkey($this->appkey);//應(yīng)用appkey
$template->set_transmissionType(1);//透傳消息類型
$template->set_transmissionContent(json_encode($listId));//透傳內(nèi)容
//注意:如果設(shè)備離線(安卓),一定要設(shè)置廠商推送,不然接收不到推送(比如華為、小米等等)
//S.title=的值為推送消息標(biāo)題,對應(yīng)5+ API中PushMessage對象的title屬性值;
//S.content=的值為推送消息內(nèi)容,對應(yīng)5+ API中PushMessage對象的content屬性值;
//S.payload=的值為推送消息的數(shù)據(jù),對應(yīng)5+ API中PushMessage對象的payload屬性值;
$intent = 'intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=此處為打包APP的包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=標(biāo)題;S.content=內(nèi)容;S.payload=數(shù)據(jù);end';
$notify = new \IGtNotify();
$notify->set_title('通知');
$notify->set_content('你有一條新消息');
$notify->set_intent($intent);
$notify->set_type(NotifyInfo_type::_intent);
$template->set3rdNotifyInfo($notify);
//下面這些是蘋果需要設(shè)置的,只要是ios系統(tǒng)的,都要設(shè)置這個,不然離線收不到
//APN高級推送
$alertmsg=new \DictionaryAlertMsg();
$alertmsg->body=$mes['content'];
$alertmsg->actionLocKey="查看";
$alertmsg->locKey=$listId['content'];
$alertmsg->locArgs=array("locargs");
$alertmsg->launchImage="launchimage";
// IOS8.2 支持
$alertmsg->title=$mes['title'];
$alertmsg->titleLocKey="測試";
$alertmsg->titleLocArgs=array("TitleLocArg");
$apn = new \IGtAPNPayload();
$apn->alertMsg=$alertmsg;
$apn->badge=0;
$apn->sound="";
$apn->add_customMsg("payload","payload");
$apn->contentAvailable=0;
$apn->category="ACTIONABLE";
$template->set_apnInfo($apn);
return $template;
}
}
調(diào)用
//個推消息推送
public function pushChat($cid=null, $type=null)
{
if(empty($cid)){
die;
}
import('getui.GeTui', EXTEND_PATH,".php");
$getui = new \GeTui($appid,$appkey,$mastersecret);
//單發(fā)測試 $cid 客戶端id 前端獲取
$getui->pushMessageToSingle($cid);
}
來源:https://www.cnblogs.com/j-jian/p/13061118.html
總結(jié)
以上是生活随笔為你收集整理的uniapp连接php,thinkphp5 对接手机uni-app的unipush推送(个推)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上甘岭战役美军出动兵力多少人?
- 下一篇: 皇太极两次血洗朝鲜最后结局如何