uniPush+Im环信消息+php(tp5)后端在线离线消息推送手机

iXIanGang / 2024-02-26 / 原文

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

 

 

public function  transfer(){
    $dataJson = file_get_contents("php://input");
    //接受发送消息
    $dataArr = json_decode($dataJson, true);

}


3.用户绑定clientId  (前段传给clientId你唯一标示)
 public function imBinding(){
    $uid = $this->user->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('错误信息');
    }
    $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(){
    $api = new \GTClient("https://restapi.getui.com","AppKey", "AppID","MasterSecret");
    $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_GT_ONLY);
    $setting->setStrategy($str);
    $push->setSettings($setting);
    $setting->setTtl(3600000); //消息有效期,走厂商消息需要设置该值
    $title = "标题";
    $content = "测试推送";
    //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_STAERAPP);//这里直接默认设置成了打开app
    if($clickType == 'payload' || $clickType == 'payload_custom'){ //自定义消息 打开APP和不打开APP
        $notification->setClickType($clickType);
        $notification->setPayload('wwww.baidu.xom');
    }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("startapp");
    $message->setNotification($notify);
    $push->setPushMessage($message);
    $push->setCid('1826b83841ce8d083713ab14797081bb');  //client_id
    $result = $api->pushApi()->pushToSingleByCid($push);  
    print_r($result);exit;
}