最近公司因为项目需要,要用PHP写一个unipush的推送。这里记录一下。
后续可能会更新定时任务去推送
一.PHP代码
请求类(封装好可直接调用)
class Req
{
public function postJson($url,$data='',$token=''){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($data),
'Cache-Control: no-cache',
'token:'.$token,
'Pragma: no-cache'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
$errorno = curl_errno($curl);
if ($errorno) {
return $errorno;
}
curl_close($curl);
return $res;
}
}
工具类(只写了在线推送)
class AppPush
{
protected $appkey;
protected $mastersecret;
protected $req;
protected $url;
protected $appid;
public function __construct()
{
$this->appid='';
$this->appkey='';
$this->mastersecret='';
$this->url='https://restapi.getui.com/v2/';
$this->req=app()->make( Req::class);
}
//获取个推的token
public function getToken()
{
//先判断一下有没有缓存,缓存有没有过期,如果有缓存那么直接返回
if(cache('pushtoken')){
return cache('pushtoken');
}
$url = $this->url.$this->appid.'/auth';
$time=time()*1000;
$data = [
'sign' => hash("sha256", $this->appkey.$time.$this->mastersecret),
"timestamp"=> $time,
"appkey"=> $this->appkey
];
$data = json_encode($data);
$res=$this->req->postJson($url,$data);
$res=json_decode($res);
if($res->code==0){//获取成功,把token存进缓存,时间为过期前10分钟
$ctime=($res->data->expire_time-$time)/1000-600;
cache('pushtoken', $res->data->token, floor($ctime));
return $res->data->token;
//这里有点小问题,跑是没有问题的,如果没有获取到token的话应该会报错,调用的时候应该加一个如果没有返回token的话报错,这里就先不写啦,要用的自己补上
}
}
//单个的推送,其它的就自己写啦.
public function pushCid($cid,$data='',$request_id=null){
//echo "2222222";
$token= $this->getToken();
$url = $this->url.$this->appid.'/push/single/cid';
$push = [
"request_id"=>$request_id??time(),
"settings"=>["ttl"=>3600000],
"audience"=>[ "cid"=>[ $cid]],
"push_message"=>[
"notification"=>[
//下面是php7点几的特性,部分低版本不能用,需要自己换成三元运算的
"title"=>$data['title']??"表情更新啦",
"body"=>$data['body']??"又更新了不少表情,来看一看么",
"click_type"=>$data['click_type']??"startapp",
"url"=>$data['url']??"cn.ibiaoqin.www",
'logo_url'=>$data['logo_url']??'',
] ]
];
$push = json_encode($push);
$res=$this->req->postJson($url,$push,$token);
$res=json_decode($res);
// echo $res;
//var_dump($res);
if($res->code==0)
return ['code'=>1,'msg'=>'推送成功'];
return ['code'=>0,'msg'=>'推送失败'];
}
public function pushCids($cids,$data='',$request_id=null){
foreach ($cids as $cid){
$this->pushCid($cid,$data,$request_id);
}
}
}
demo
class Index
{
public static function index($cid)
{
//echo "1111111";
$push=app()->make(AppPush::class);
//$cid='cbd386aba3a4c99163fdef0f11960d0a';
$data=[];
$data['title']='欢迎来到拓金商城';
//echo 11111;
$push->pushCid($cid,$data);
}
public static function index1()
{
echo "1111111";
$push=app()->make(AppPush::class);
$cids=['cbd386aba3a4c99163fdef0f11960d0a','56926ba80cbbd53ce2dd1dc78fc10c11'];
$data=[];
//$data['title']='test';
var_dump( $push->pushCids($cids,$data));
}
public function hello($name = '1')
{
return $name ?? '123';
}
}
二.如何与前端对接
前端自定义基座调试
hbuilderx点击运行选择制作自定义基座,然后选择DCloud老版证书打包(也可以选择自有证书)进行打包。在这之前,需要去Dcloud后台创建应用,报名需和打包的报名一致,如果使用dcloud证书打包的话就用他们的签名,不然需使用自己证书相匹配的签名。然后点击dcloud平台的unipush,选择所创建的应用
点击应用概览把appid,appkey,masterkey复制到后端代码。然后测试推送即可。
测试时,点击hbuilderx的运行->运行基座选择->自定义基座。需注意每次卸载重装之后的cid都不一样,需实时获取。