1.引入unipush包
composer require getuilaboratory/getui-pushapi-php-client-v2
如果报错
composer require getuilaboratory/getui-pushapi-php-client-v2 dev-master
2.设置环信消息回调接口通过json接口
设置环信消息回调需要登陆:https://console.easemob.com/app/im-service/func/callback
![]()
![]()
2.1拿到发送消息回调信息
public function transfer(){
$dataJson = file_get_contents("php://input");
//接受发送消息
$dataArr = json_decode($dataJson, true);
}
3.用户绑定clientId (前段传给clientId设备)
public function imBinding(){
// $uid = $this->user->id;
$uid = $this->request->param('id');
$client_id = $this->request->param('client_id');
if (!$client_id){
$this->error("缺少client_id参数!");
}
$info = $this->UserModel->where('id',$uid)->find();
if (empty($info)){
$this->error('错误信息');
}
$user = $this->UserModel->where('client_id',$client_id)->select();
if ($user){
$this->UserModel->where('client_id',$client_id)->update(['client_id' => '']);
}
$data = [
'client_id' => $client_id,
'updatetime' => time(),
];
$rs = $this->UserModel->where('id',$uid)->update($data);
if ($rs){
$this->success('请求成功!');
}
$this->error('报错报错!');
}
4.消息推送
注:AppKey,AppID,MasterSecret在https://dev.dcloud.net.cn/pages/common/login申请
public function pushSend($client_id,$title,$content,$url){
$api = new \GTClient("https://restapi.getui.com","VOHGFoiutq7sQo7fmxx2B1", "kZusa4Kptd9tK4ku4wcg53","bWGYMd6lTk84I8BXQumYm4");
$push = new \GTPushRequest();
$push->setRequestId(md5(time() . mt_rand(1, 9999999)));//请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
$message = new \GTPushMessage();//个推推送
$channel = new \GTPushChannel();//厂商推送
//配置推送条件
$setting = new \GTSettings();//settings 推送条件设置
$str = new \GTStrategy();//strategy 厂商下发策略选择
//默认所有通道的策略选择1-4
//1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道;
//2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线;
//3: 表示该消息只通过个推通道下发,不考虑用户是否在线;
//4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
//其中名称可填写: ios、st、hw、xm、vv、mz、op。
$str->setDefault($str::STRATEGY_THIRD_FIRST);
$setting->setStrategy($str);
$push->setSettings($setting);
$setting->setTtl(3600000); //消息有效期,走厂商消息需要设置该值
//ios厂商通道参数设置
$iosDto = new \GTIos();
$aps = new \GTAps();//推送通知消息内容
$alert = new \GTAlert();//通知消息
$alert->setTitle($title);//通知消息标题
$alert->setBody($content);//通知消息内容
$aps->setContentAvailable(0);
$aps->setSound("default");
$aps->setAlert($alert);
$iosDto->setAps($aps);
$iosDto->setType("notify");
$channel->setIos($iosDto);
//android厂商通道参数设置
$androidDto = new \GTAndroid();
$ups = new \GTUps();//推送通知消息内容
$notification = new \GTThirdNotification();//通知消息
$notification->setTitle($title);
$notification->setBody($content);
$clickType = 'payload';
$notification->setClickType($notification::CLICK_TYPE_PAYLOAD);//这里直接默认设置成了打开app
if($clickType == 'payload' || $clickType == 'payload_custom'){ //自定义消息 打开APP和不打开APP
$notification->setClickType($clickType);
$notification->setPayload($url);
}else if($clickType == 'url'){ //打开URL
$notification->setClickType($clickType);
$notification->setUrl('');
}else if($clickType == 'intent'){ //打开特定页面
$notification->setClickType($clickType);
$notification->setIntent('');
}else{
$notification->setClickType($clickType);
}
$ups->setNotification($notification);
$androidDto->setUps($ups);
$channel->setAndroid($androidDto);
//设置厂商推送消息参数
$push->setPushChannel($channel);
//个推参数设置
$notify = new \GTNotification();//消息设置
$notify->setTitle($title);//通知标题 长度 ≤ 50
$notify->setBody($content);//通知内容 长度 ≤ 256
//1、intent:打开应用内特定页面url:打开网页地址。
//2、payload:自定义消息内容启动应用。
//3、payload_custom:自定义消息内容不启动应用。
//4、startapp:打开应用首页。
//5、none:纯通知,无后续动作
$notify->setClickType("payload");
$notify->setPayload($url);
$message->setNotification($notify);
$push->setPushMessage($message);
$push->setCid($client_id);
$result = $api->pushApi()->pushToSingleByCid($push);
return $result['msg'];
}