thinkphp5 对接手机uni-app的unipush推送(个推)

    PHP对接unipush(推送)也是要按照个推的文档进行对接(其实unipush用的服务就是个推的,前者应该是和个推谈了合作,个推的厂商推送服务免费开放给uni的开发人员)

    先去个推官网下SDK:http://docs.getui.com/download.html

    个推文档:http://docs.getui.com/getui/server/php/push/

    

放入项目的extend文件夹(第三方扩展),并在getui的demo文件同级创建一个GeTui.php文件,用来实例化并调用,class代码贴在图片后面

  

 

因为需求问题,我项目只有用到单用户的推送,所以这边只贴我测试并通过的代码,没有多推的示例

这里需要注意的是:消息内容决定了系统的通知栏的通知等级,如果通知栏一直没有消息,可以去查一下各自手机厂商的消息等级,对应修改文字

<?php

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);//设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据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 ("<br><br>");

        }catch(RequestException $e){
            $requstId =e.getRequestId();
            //失败时重发
            $rep = $igt->pushMessageToSingle($message, $target,$requstId);
            var_dump($rep);
            echo ("<br><br>");
        }
    }

    //透传消息模板
    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);//应用appid
        $template->set_appkey($this->appkey);//应用appkey
        $template->set_transmissionType(1);//透传消息类型
        $template->set_transmissionContent(json_encode($listId));//透传内容

        //注意:如果设备离线(安卓),一定要设置厂商推送,不然接收不到推送(比如华为、小米等等)
        //S.title=的值为推送消息标题,对应5+ API中PushMessage对象的title属性值;
        //S.content=的值为推送消息内容,对应5+ API中PushMessage对象的content属性值;
        //S.payload=的值为推送消息的数据,对应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=标题;S.content=内容;S.payload=数据;end';

        $notify = new \IGtNotify();
        $notify->set_title('通知');
        $notify->set_content('你有一条新消息');
        $notify->set_intent($intent);
        $notify->set_type(NotifyInfo_type::_intent);
        $template->set3rdNotifyInfo($notify);

        //下面这些是苹果需要设置的,只要是ios系统的,都要设置这个,不然离线收不到
        //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;
    }


}

 

调用

    //个推消息推送
    public function pushChat($cid=null, $type=null)
    {
        if(empty($cid)){
            die;
        }

        import('getui.GeTui', EXTEND_PATH,".php");
        $getui = new \GeTui($appid,$appkey,$mastersecret);
        //单发测试 $cid 客户端id 前端获取
        $getui->pushMessageToSingle($cid);

    }

 

posted @ 2020-06-07 16:50  贱贱丶  阅读(4592)  评论(0编辑  收藏  举报