zoukankan      html  css  js  c++  java
  • tp5 整合 个推

    这里因为业务需要使用推送功能 uni 里面前端集成了个推 所以选择了个推来做推送.

    个推的官方文档地址: http://docs.getui.com/getui/server/php/start/

    在个推官方没有找到 composer 包

    只能手动的将 sdk 放到了项目中

    sdk下载地址:http://www.getui.com/download/docs/getui/server/GETUI_PHP_SDK_4.1.0.0.zip (官方文档里有建议从官方文档下载) 

    这里的用的是tp5框架 laravel yii 和 ci 中需要用到的朋友 可以根据各自的框架调整下 自行加载到这个类调整下配置读取即可 

    下载解压 把它放到 项目根路径extend 文件夹内即可

    这里 个推的demo 都是 以函数的方式 呈现的 这里我自己将他整理成了一个类 GeTui.php

    <?php
    namespace getui;
    
    class GeTui
    {
        private $host = 'http://sdk.open.api.igexin.com/apiex.htm';
        //测试
        private $appkey = '';
        private $appid = '';
        private $mastersecret = '';
    
        private function init()
        {
            // header("Content-Type: text/html; charset=utf-8");
            $this->appid = config('getui.appid');
            $this->appkey = config('getui.appkey');
            $this->mastersecret = config('getui.mastersecret');
            $this->host = config('getui.host');
    
        }
    
        public function __construct()
        {
            $this->init();
            $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');
        }
    
        //服务端推送接口,支持三个接口推送
        //1.PushMessageToSingle接口:支持对单个用户进行推送
        //2.PushMessageToList接口:支持对多个用户进行推送,建议为50个用户
        //3.pushMessageToApp接口:对单个应用下的所有用户进行推送,可根据省份,标签,机型过滤推送
        //单推接口案例
        function pushMessageToSingle($cid){
            $igt = new IGeTui($this->host,$this->appkey,$this->mastersecret);
    
            //消息模版:
            // 通知栏消息模板
            $template = $this->IGtNotificationTemplateDemo();
            // 穿透消息模板
            // $template = $this->IGtTransmissionTemplateDemo();
    
    
            //定义"SingleMessage"
            $message = new IGtSingleMessage();
    
            $message->set_isOffline(true);//是否离线
            $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);
        //    $target->set_alias(Alias);
    
            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(){
            $template =  new IGtTransmissionTemplate();
            $template->set_appId($this->appid); //应用appid
            $template->set_appkey($this->appkey); //应用appkey
            //透传消息类型
            $template->set_transmissionType(2);
            $payload = [
                'title' => '测试',
                'content' => '1111',
                'payload' => '测试参数'
            ];
            //透传内容
            $template->set_transmissionContent(json_encode($payload));
            // $template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
            //这是老方法,新方法参见iOS模板说明(PHP)*/
            //$template->set_pushInfo("actionLocKey","badge","message",
            //"sound","payload","locKey","locArgs","launchImage");
    
            //  APN高级推送
            // $apn = new IGtAPNPayload();
            // $alertmsg=new DictionaryAlertMsg();
            // $alertmsg->body="body";
            // $alertmsg->actionLocKey="ActionLockey";
            // $alertmsg->locKey="LocKey";
            // $alertmsg->locArgs=array("locargs");
            // $alertmsg->launchImage="launchimage";
            return $template;
        }
    
        //通知栏消息 (通知栏显示 点击启动应用)
        function IGtNotificationTemplateDemo(){
            $template =  new IGtNotificationTemplate();
            $template->set_appId($this->appid); //应用appid
            $template->set_appkey($this->appkey); //应用appkey
            $template->set_transmissionType(1);//透传消息类型
            $template->set_transmissionContent("测试离线");//透传内容
            $template->set_title("呵呵");//通知栏标题
            $template->set_text("呵呵最新版点击下载");//通知栏内容
            $template->set_logo("http://wwww.igetui.com/logo.png");//通知栏logo
            $template->set_isRing(true);//是否响铃
            $template->set_isVibrate(true);//是否震动
            $template->set_isClearable(true);//通知栏是否可清除
            //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
            return $template;
        }
    
        //群推接口案例
        public function pushMessageToApp(){
            $igt = new IGeTui($this->host,$this->appkey,$this->mastersecret);
    
            $template = $this->IGtNotificationTemplateDemo();
            //个推信息体
            //基于应用消息体
            $message = new IGtAppMessage();
            $message->set_isOffline(true);
            $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒,例,两个小时离线为3600*1000*2
            $message->set_data($template);
        //    $message->setPushTime("201808011537");
            $appIdList=array($this->appid);
            $phoneTypeList=array('ANDROID');
            $provinceList=array('上海');
            $tagList=array('中文');
            $age = array("0000", "0010");
    
            //推送条件
            // $cdt = new AppConditions();
            // 手机类型
            // $cdt->addCondition(AppConditions::PHONE_TYPE, $phoneTypeList);
            // 地区
            // $cdt->addCondition(AppConditions::REGION, $provinceList);
            // 标签
            // $cdt->addCondition(AppConditions::TAG, $tagList);
            // 年龄?
            // $cdt->addCondition("age", $age);
            // $message->set_conditions($cdt);
    
            $message->set_appIdList($appIdList);
    
            $rep = $igt->pushMessageToApp($message);
    
            var_dump($rep);
            echo ("<br><br>");
        }
    
        //通知栏显示 点击跳转url
        function IGtLinkTemplateDemo(){
            $template =  new IGtLinkTemplate();
            $template ->set_appId($this->appid);//应用appid
            $template ->set_appkey($this->appkey);//应用appkey
            $template ->set_title("测试群发消息");//通知栏标题
            $template ->set_text("点击就送66个老铁666");//通知栏内容
            $template ->set_logo("http://wwww.igetui.com/logo.png");//通知栏logo
            $template ->set_isRing(true);//是否响铃
            $template ->set_isVibrate(true);//是否震动
            $template ->set_isClearable(true);//通知栏是否可清除
            $template ->set_url("http://www.igetui.com/");//打开连接地址
            //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
            return $template;
        }
    
    }

    将他放到 demo.php 同级目录 

    个推的相关配置 我是写在了 

    tp5项目applicationextragetui.php 

    可以看到我在 GeTui.php 这个类文件里 初始化的时候 进行了读取 和 加载相关文件

    <?php
    
    
    return [
        //  +---------------------------------
        //  个推相关配置
        //  +---------------------------------
    
    
        //AppID:由IGetui管理页面生成,是您的应用与SDK通信的标识之一,每个应用都对应一个唯一的AppID。
        'appid' => 'XXX',
    
        //AppSecret:第三方客户端个推集成鉴权码,用于验证第三方合法性。在客户端集成SDK时需要提供。
        'AppSecret' => 'XXX',
    
        //AppKey:预先分配的第三方应用对应的Key,是您的应用与SDK通信的标识之一。
        'appkey' => 'XXX',
    
        //MasterSecret:个推服务端API鉴权码,用于验证调用方合法性。在调用个推服务端API时需要提供。(请妥善保管,避免通道被盗用)。
        'mastersecret' => "XX",
    
        //
        'host' => "http://sdk.open.api.igexin.com/apiex.htm",
    
    
    ];

    将其替换成自己的即可

    后面在需要调用的地方使用

        //个推案例接口
        public function getui()
        {
            //实例化之前写的类
            $getui = new getuiGeTui();
            //单发测试 $cid 客户端id 前端获取 
            // $getui->pushMessageToSingle($cid);
            // 群发测试
            $getui->pushMessageToApp();
            // dump($getui);die;
        }

    下载个推注册时 生成的app 即可后端调试

     到了这里基本就可以 开心的测试接口了 

    //这个是我案例中 单发接口调用的模板 群发我这里用的也是这个模板
    //相关的其他模板可以在demo 中 找一下 根据自己需求调整 
    $template = $this->IGtNotificationTemplateDemo(); 

    这里的是生成了一个模板 影响到 消息推送到后的 下一步动作 根据自己的业务需求编辑和选择即可

    这里放其他使用其他模板的案例

    //通知栏显示 点击跳转url
    $template = $this->IGtLinkTemplateDemo()

    安卓 推送通知 和 穿透消息都能接收到 

    ios 只能接收到 穿透消息

  • 相关阅读:
    谷歌火狐浏览器限制的端口
    Web Api 内部数据思考 和 利用http缓存优化 Api
    Web Api 端点设计 与 Oauth
    FI-FBV0
    Others-SAP hybris 介绍
    ABAP-表修改日志存储
    TR-银行通信相关文档
    ABAP-Eclipse ADT中创建ABAP CDS视图
    TR-业务流程图
    FPM-OVP增强实例-银行账户
  • 原文地址:https://www.cnblogs.com/y734290209/p/10774884.html
Copyright © 2011-2022 走看看