<?php
/**
 * Created by PhpStorm.
 * User: hujun
 * Date: 2017/12/24
 * Time: 21:09
 */

namespace app\api\untils;


use think\Loader;

//http的域名
define('HOST','http://sdk.open.api.igexin.com/apiex.htm');

//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
define('APPKEY','m6kO8mg0rs85RJD1lW4Gv8');
define('APPID','Cp63kT0yXMAmxVmekIQVI5');
define('MASTERSECRET','02DunI3DGL60rXkud74YZ8');
class GeTuiUntils {
    /**
     * 个推
     * zhuwei
     * 2017-11-06
     */
    function push_message_for_one()
    {

        $data=$this->request->data;
        //$this->changlianjie($data);
        $Agentdata = $this->Agent->findById($data['agent_id']);
        $cid =$Agentdata['Agent']['device_id'];

        $igt = new IGeTui(TL_GETUI_HOST, TL_GETUI_APPKEY, TL_GETUI_MASTERSECRET);

        //模板
        $template = $this->onli_all_template($data['touchuan'], $data['black_title'],$data['content']);
        $this->log($data['black_title']);

        //个推信息体
        $message = new IGtSingleMessage();
        $message->set_isOffline(true); //是否离线
        $message->set_offlineExpireTime(3600 * 12 * 1000); //离线时间
        $message->set_data($template); //设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        //接收方
        $target = new IGtTarget();
        $target->set_appId(TL_GETUI_APPID);
        $target->set_clientId($cid);
        //var_dump($this->request->data);
        //var_dump(TL_GETUI_APPID);
        //$target->set_alias(Alias);
        try {
            $rep = $igt->pushMessageToSingle($message, $target);
            $this->log("======pushnotice_return");
            $this->log($rep);
            //var_dump($rep);
            echo("<br><br>");
        } catch (RequestException $e) {
            $requstId = $e . getRequestId();
            $rep = $igt->pushMessageToSingle($message, $target, $requstId);
            //var_dump($rep);
            echo("<br><br>");
        }
    }


    /**
     * 群推
     * zhuwei
     * 2017-11-06
     */
    function push_message_for_all()
    {
        //Configure::write('debug', 2);
        $data = $this->request->data;

        $igt = new IGeTui(TL_GETUI_HOST, TL_GETUI_APPKEY, TL_GETUI_MASTERSECRET);

        $template = $this->onli_all_template($data['touchuan'], $data['black_title'], $data['content']);
        //个推信息体
        //基于应用消息体
        $message = new IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(3600 * 12 * 1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        //$message->set_speed(1);// 设置群推接口的推送速度，单位为条/秒，例如填写100，则为100条/秒。仅对指定应用群推接口有效。
        $appIdList = array(TL_GETUI_APPID);
        //$phoneTypeList=array($data['android_or_ios']);
        //$cdt = new AppConditions();
        //$cdt->addCondition(AppConditions::PHONE_TYPE, $phoneTypeList);
        $message->set_appIdList($appIdList);
        //$message->set_conditions($cdt);
        $rep = $igt->pushMessageToApp($message);
        //var_dump($rep);
        echo("<br><br>");

    }





    /**
     * 推送模板
     * zhuwei
     * 2017-11-06
     */
    function onli_all_template($touchuan, $black_title, $content)
    {
        //$this->changlianjie($content);
        $template = new IGtTransmissionTemplate();
        $template->set_appId(TL_GETUI_APPID);//应用appid
        $template->set_appkey(TL_GETUI_APPKEY);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent($touchuan);   //透传内容
        //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
        /*
        //如下有两个推送模版，一个简单一个高级，可以互相切换使用。此处以高级为例，所以把简单模版注释掉。
        //APN简单推送
          $apn = new IGtAPNPayload();
          $alertmsg=new SimpleAlertMsg();
          $alertmsg->alertMsg="";
          $apn->alertMsg=$alertmsg;
          $apn->badge=2;
          $apn->sound="";
          $apn->add_customMsg("payload","payload");
          $apn->contentAvailable=1;
          $apn->category="ACTIONABLE";
          $template->set_apnInfo($apn);
        //2）离线透传参数设置

        //而 从 $apn = new IGtAPNPayload();这一行开始，构建的都是针对ios下app离线的推送
        //其中 $alertmsg->body="body"; 对应的是在线透传的content
        $alertmsg->locKey="LocKey"; 对应的是在线透传的title
        $alertmsg->title="水培喝水"; 对应的也是在线透传的title

        //然后 $apn->add_customMsg("payload","阿波罗度上市"); 这是用来传送自定义参数的，我们来改成我们想要的
        $apn->add_customMsg("id","1");
        $apn->add_customMsg("type","新闻");
        */

        //APN高级推送
        $apn = new IGtAPNPayload();
        $alertmsg = new DictionaryAlertMsg();
        $alertmsg->body = $content;//对应的是在线透传的content
        $alertmsg->actionLocKey = "打开同联商业";//滑动打开app
        $alertmsg->locKey = $content;//锁屏提示下框
        /*
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
        iOS8.2 支持
        */
        $alertmsg->title = $black_title;//标题 锁屏标题
        $alertmsg->titleLocKey = $black_title;//下拉通知上部框框
        $alertmsg->titleLocArgs = array("TitleLocArg");

        $apn->alertMsg = $alertmsg;
        $apn->badge = 1;//应用icon上显示的数字
        $apn->sound = "";
        /*
        $apn->add_customMsg("payload","阿波罗度上市");
        $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        */
        $template->set_apnInfo($apn);
        return $template;
    }



    function pushMessageToApp(){
//        Loader::import('IGeTui',EXTEND_PATH.'GETUI_PHP_SDK');

        include_once(EXTEND_PATH.'GETUI_PHP_SDK'.DS.'IGt.Push.php');
        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);

        //定义透传模板，设置透传内容，和收到消息是否立即启动启用
        $template = $this->IGtNotificationTemplateDemo();
        //$template = IGtLinkTemplateDemo();
        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        $message = new \IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);

        $appIdList=array(APPID);
        $phoneTypeList=array('ANDROID');
        $provinceList=array('浙江');
        $tagList=array('haha');
        //用户属性
        //$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_appIdList($appIdList);
        //$message->set_conditions($cdt->getCondition());

        $rep = $igt->pushMessageToApp($message,"任务组名");

        var_dump($rep);
        echo ("<br><br>");
    }
    function IGtNotificationTemplateDemo(){
        $template =  new \IGtNotificationTemplate();
        $template->set_appId(APPID);                   //应用appid
        $template->set_appkey(APPKEY);                 //应用appkey
        $template->set_transmissionType(1);            //透传消息类型
        $template->set_transmissionContent("测试离线");//透传内容
        $template->set_title("个推");                  //通知栏标题
//        $template->set_text("个推最新版点击下载");     //通知栏内容
        $template->set_text('【张三】报备了【肯德基】');     //通知栏内容
        $template->set_logo("");                       //通知栏logo
        $template->set_logoURL("");                    //通知栏logo链接
        $template->set_isRing(true);                   //是否响铃
        $template->set_isVibrate(true);                //是否震动
        $template->set_isClearable(true);              //通知栏是否可清除

        return $template;
    }
}