namespace IGeTuics;
//增加ClientId可自定义别名功能
header("Content-Type: text/html; charset=utf-8");
require_once(dirname(FILE) . '/' . 'IGt.Push.php');
require_once(dirname(FILE) . '/' . 'IGt.Batch.php');
class alias_demo{
private $APPKEY = 'LlDnps2T6j5Oe3Qi9DdJhA'; // appkey
private $APPID = 'OFhD1n1bLq8uUIraSGv5pA'; // appid
private $MASTERSECRET = 'DG1BieV7b07ez3lJQniQX7'; // master_secret
private $CID = '5bca081ec28f85f91530bf60f55d9870'; // 设备标识
private $Alias = ''; // 用户别名
private $HOST = 'http://sdk.open.api.igexin.com/apiex.htm';

public function __construct($app_key=null, $master_secret=null,$app_id=null) {
    // if ($app_key) $this->APPKEY = $app_key;
    // if ($master_secret) $this->MASTERSECRET = $master_secret;
    // if ($app_id) $this->APPID = $app_id;
}

//pushMessageToSingle();

// pushMessageToList();    // 多推接口

//aliasBind();    // ClientID与别名绑定

//aliasBatch(); // 多个ClientID,使用同一别名绑定

//queryCID();   // 根据别名查询ClientId

//queryAlias(); // 根据ClientId查询别名

//aliasUnBind();  // 解除ClientId别名绑定

//aliasUnBindAll();     // 解除所有ClientId别名绑定

/**
 * ClientID与别名绑定
 * [aliasBind description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $cid   [description] 用户设备标识
 * @param    [type]     $alias [description] 用户别名
 * @return   [type]            [description]
 */
function aliasBind($cid,$alias){
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    $rep = $igt->bindAlias($this->APPID,$alias,$cid);
    return $rep;
}
/**
 * 多个ClientID,使用同一别名绑定
 * [aliasBatch description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $alias    [description] 别名
 * @param    [type]     $cid_list [description] cid数组
 * @return   [type]               [description]
 */
function aliasBatch($alias,$cid_list){
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    $targetList = [];
    foreach ($cid_list as $key => $value) {
        $target = new \IGtTarget();
        $target->set_clientId($value);
        $target->set_alias($alias);
        $targetList[] = $target;
    }
    $rep = $igt->bindAliasBatch($this->APPID,$targetList);
    return $rep;
}
/**
 * 根据别名查询ClientId
 * [queryCID description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $alias [description] 用户别名
 * @return   [type]            [description]
 */
function queryCID($alias){
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    $rep = $igt->queryClientId($this->APPID,$alias);
    return $rep;
}
/**
 * 根据ClientId查询别名
 * [queryAlias description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $cid [description] 用户设备标识
 * @return   [type]          [description]
 */
function queryAlias($cid){
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    $rep = $igt->queryAlias($this->APPID,$cid);
    return $rep;
}
/**
 * 解除ClientId别名绑定
 * [aliasUnBind description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $alias [description] 用户别名
 * @param    [type]     $cid   [description] 用户设备标识
 * @return   [type]            [description]
 */
function aliasUnBind($alias,$cid){
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    $rep = $igt->unBindAlias($this->APPID,$alias,$cid);
    return $rep;
}
/**
 * 解除所有ClientId别名绑定
 * [aliasUnBindAll description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $cid   [description] 用户设备标识
 * @param    [type]     $alias [description] 用户别名
 * @return   [type]            [description]
 */
function aliasUnBindAll($cid,$alias){
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    $rep = $igt->unBindAliasAll($this->APPID,$alias,$cid);
    return $rep;
}
/**
 * 绑定cid标签
 * [setClientTag description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $cid      [description] 用户设备标识
 * @param    [type]     $tag_list [description] 标签数组
 */
function setClientTag($cid,$tag_list){
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    $rep = $igt->setClientTag($this->APPID,$cid,$tag_list);
    return $rep;
}

//
//服务端推送接口,支持三个接口推送
//1.pushMessageToSingle 接口:支持对单个用户进行推送
//2.pushMessageToList 接口:支持对多个用户进行推送,建议为50个用户
//3.pushMessageToApp 接口:对单个应用下的所有用户进行推送,可根据省份,标签,机型过滤推送
//
/**
 * 统一返回数据
 * [ret_data description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $rep [description]
 * @return   [type]          [description]
 */
function ret_data($rep){
    $ret = [];
    $ret['code'] = 0;
    if ($rep['result'] == 'ok') {
        $ret['code'] = 1;
        $ret['msg']  = '发送成功!';
    }elseif ($rep['result'] == 'sign_error') {
        $ret['msg'] = 'sign鉴权失败!';
    }elseif ($rep['result'] == 'AppidNoMatchAppKey') {
        $ret['msg'] = 'Appid和AppKey不匹配!';
    }elseif ($rep['result'] == 'PushTotalNumOverLimit') {
        $ret['msg'] = '推送总量超限!';
    }elseif ($rep['result'] == 'TokenMD5NoUsers') {
        $ret['msg'] = 'cid没有查询到相关用户!';
    }elseif ($rep['result'] == 'flow_exceeded') {
        $ret['msg'] = '流控!';
    }elseif ($rep['result'] == 'TooFrequent') {
        $ret['msg'] = '频控!';
    }elseif ($rep['result'] == 'AppidError') {
        $ret['msg'] = 'appId有误!';
    }elseif ($rep['result'] == 'transmissionContent length overlimit') {
        $ret['msg'] = '透传内容长度限制!';
    }elseif (!strstr($rep['result'],'is not null')) {
        $ret['msg'] = '参数校验失败!';
    }elseif (!strstr($rep['result'],'Contain Sensitive Word Error')) {
        $ret['msg'] = '敏感词受限!';
    }elseif ($rep['result'] == 'AliasNotBind') {
        $ret['msg'] = '别名没有绑定cid!';
    }elseif ($rep['result'] == 'data_too_big') {
        $ret['msg'] = '整体数据大小超限!';
    }elseif ($rep['result'] == 'target overLimit') {
        $ret['msg'] = '批量数量超限!';
    }elseif ($rep['result'] == 'NullMsgCommon') {
        $ret['msg'] = 'contentId对于的消息体过期或者不存在!';
    }elseif ($rep['result'] == 'TaskIdHasBeanCanceled') {
        $ret['msg'] = '任务已经被取消!';
    }elseif ($rep['result'] == 'NOTarget') {
        $ret['msg'] = 'cidList为空!';
    }elseif ($rep['result'] == 'PushMsgToListTimesOverLimit') {
        $ret['msg'] = 'toList推送总量超限!';
    }elseif ($rep['result'] == 'UndefinedContentId') {
        $ret['msg'] = '未找到公共体!';
    }elseif ($rep['result'] == 'DuplicatedContentId') {
        $ret['msg'] = '已经调用过!';
    }elseif ($rep['result'] == 'OverLimit') {
        $ret['msg'] = 'tag数量超限!';
    }elseif ($rep['result'] == 'TagInvalidOrNoAuth') {
        $ret['msg'] = '没有有效的tag!';
    }elseif ($rep['result'] == 'RepeatedContent') {
        $ret['msg'] = '内容重复!';
    }elseif ($rep['result'] == 'PushMsgToAppTimesOverLimit') {
        $ret['msg'] = 'App群推次数超限!';
    }else{
        $ret['msg'] = '未知错误!';
    }
    return $ret;
}
/**
 * 单推接口案例
 * [pushMessageToSingle description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $cid     [description] 用户设备标识
 * @param    [type]     $content [description] 透传内容
 * @param    [type]     $title   [description] 通知栏标题
 * @param    [type]     $text    [description] 通知栏内容
 * @param    [type]     $logo    [description] 通知栏logo
 * @return   [type]              [description]
 */
function pushMessageToSingle($cid,$content,$title,$text,$logo){
    $cid = $this->CID;
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);

    // 消息模版:
    // 1.TransmissionTemplate:透传功能模板
    // 2.LinkTemplate:通知打开链接功能模板
    // 3.NotificationTemplate:通知透传功能模板
    // 4.NotyPopLoadTemplate:通知弹框下载功能模板

    // $template = IGtNotyPopLoadTemplateDemo();
    // $template = IGtLinkTemplateDemo();
    $template = $this->IGtNotificationTemplateDemo($content,$title,$text,$logo);    
    // $template = $this->IGtTransmissionTemplateDemo();
    //个推信息体
    $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($this->APPID);
    $target->set_clientId($this->CID);
    // $target->set_alias(ALIAS);
    $rep = $igt->pushMessageToSingle($message,$target);
    return $this->ret_data($rep);
}
/**
 * 群推
 * [pushMessageToApp description]
 * @Author   念天地之悠悠
 * @DateTime 2020-03-19
 * @param    [type]     $content   [description] 穿透内容
 * @param    [type]     $title     [description] 通知标题
 * @param    [type]     $text      [description] 通知栏内容
 * @param    [type]     $logo      [description] 通知栏logo
 * @param    [type]     $phoneType [description] 手机机型数组 ANDROID IOS
 * @param    [type]     $province  [description] 地区编号数组
 * @param    [type]     $tag       [description] 设备标签数组
 * @return   [type]                [description]
 */
function pushMessageToApp($content,$title,$text,$logo,$phoneType=null,$province=null,$tag=null){
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    $template = $this->IGtNotificationTemplateDemo($content,$title,$text,$logo);    
    $time = (int)date('YmdHi');
    //个推信息体
    //基于应用消息体
    $message = new \IGtAppMessage();
    $message->set_isOffline(true); 
    $message->set_offlineExpireTime($time); //在用户设定的时间点进行推送,格式为年月日时分
    $message->set_speed(100);               //定速推送,设置setSpeed为100,则全量送时个推控制下发速度在100条/秒左右。
    $message->set_PushNetWorkType(0);       //设置是否根据WIFI推送消息,2为4G/3G/2G,1为wifi推送,0为不限制推送,在wifi条件下能帮用户充分节省流量
    $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒,例,两个小时离线为3600*1000*2
    $message->set_data($template);
    $appIdList     = array($this->APPID);
    if (!empty($phoneType)) {
        $phoneTypeList = $phoneType;
    }
    if (!empty($province)) {
        $provinceList = $province;
    }
    if (!empty($tag)) {
        $tagList = $tag;
    }
    $cdt = new \AppConditions();
    if (!empty($phoneType)) {
        $cdt->addCondition3('phoneType', $phoneTypeList);
    }
    if (!empty($province)) {
        $cdt->addCondition3('region', $provinceList);
    }
    if (!empty($tag)) {
        $cdt->addCondition3('tag', $tagList); 
    }
    $message->set_appIdList($appIdList);
    $message->set_conditions($cdt);
    $rep = $igt->pushMessageToApp($message);
    return $this->ret_data($rep);
}
//多推接口案例
function pushMessageToList(){
    putenv("needDetails=true");
    $igt = new \IGeTui($this->HOST,$this->APPKEY,$this->MASTERSECRET);
    //消息模版:
    // 1.TransmissionTemplate:透传功能模板
    // 2.LinkTemplate:通知打开链接功能模板
    // 3.NotificationTemplate:通知透传功能模板
    // 4.NotyPopLoadTemplate:通知弹框下载功能模板

    //$template = IGtNotyPopLoadTemplateDemo();
    //$template = IGtLinkTemplateDemo();
    $template = $this->IGtNotificationTemplateDemo();
    // $template = $this->IGtTransmissionTemplateDemo();
    //个推信息体
    $message = new \IGtListMessage();
    $message->set_isOffline(true);//是否离线
    $message->set_offlineExpireTime(3600*12*1000);//离线时间
    $message->set_data($template);//设置推送消息类型
    //$message->set_PushNetWorkType(0); //设置是否根据WIFI推送消息,1为wifi推送,0为不限制推送
    $contentId = $igt->getContentId($message,'toList');
    //$contentId = $igt->getContentId($message,"toList任务别名功能"); //根据TaskId设置组名,支持下划线,中文,英文,数字

    //接收方1
    $target1 = new \IGtTarget();
    $target1->set_appId($this->APPID);
    //$target1->set_clientId($this->CID);
    $target1->set_alias($this->ALIAS);

    $targetList[] = $target1;

    $rep = $igt->pushMessageToList($contentId, $targetList);

    var_dump($rep);
    echo ("

"); } //所有推送接口均支持四个消息模板,依次为通知弹框下载模板,通知链接模板,通知透传模板,透传模板 //注:IOS离线推送需通过APN进行转发,需填写 pushInfo 字段,目前仅不支持通知弹框下载功能 // 弹框下载模板 function IGtNotyPopLoadTemplateDemo(){ $template = new \IGtNotyPopLoadTemplate(); $template ->set_appId($this->APPID);//应用appid $template ->set_appkey($this->APPKEY);//应用appkey //通知栏 $template ->set_notyTitle("个推");//通知栏标题 $template ->set_notyContent("个推最新版点击下载");//通知栏内容 $template ->set_notyIcon("");//通知栏logo $template ->set_isBelled(true);//是否响铃 $template ->set_isVibrationed(true);//是否震动 $template ->set_isCleared(true);//通知栏是否可清除 //弹框 $template ->set_popTitle("弹框标题");//弹框标题 $template ->set_popContent("弹框内容");//弹框内容 $template ->set_popImage("");//弹框图片 $template ->set_popButton1("下载");//左键 $template ->set_popButton2("取消");//右键 //下载 $template ->set_loadIcon("");//弹框图片 $template ->set_loadTitle("地震速报下载"); $template ->set_loadUrl("http://dizhensubao.igexin.com/dl/com.ceic.apk"); $template ->set_isAutoInstall(false); $template ->set_isActived(true); return $template; } //通知链接模板 function IGtLinkTemplateDemo(){ $template = new \IGtLinkTemplate(); $template ->set_appId($this->APPID);//应用appid $template ->set_appkey($this->APPKEY);//应用appkey $template ->set_title("标题");//通知栏标题 $template ->set_text("通知内容");//通知栏内容 $template ->set_logo("");//通知栏logo $template ->set_isRing(true);//是否响铃 $template ->set_isVibrate(true);//是否震动 $template ->set_isClearable(true);//通知栏是否可清除 $template ->set_url("http://www.igetui.com/");//打开连接地址 // iOS推送需要设置的pushInfo字段 //$template ->set_pushInfo($actionLocKey,$badge,$message,$sound,$payload,$locKey,$locArgs,$launchImage); //$template ->set_pushInfo("",2,"","","","","",""); return $template; } // 应用消息模板 function IGtNotificationTemplateDemo($content,$title,$text,$logo){ $template = new \IGtNotificationTemplate(); $template->set_appId($this->APPID);//应用appid $template->set_appkey($this->APPKEY);//应用appkey $template->set_transmissionType(1);//透传消息类型 $template->set_transmissionContent($content);//透传内容 $template->set_title($title);//通知栏标题 $template->set_text($text);//通知栏内容 // $template->set_logo("http://wwww.igetui.com/logo.png");//通知栏logo $template->set_logo($logo);//通知栏logo $template->set_isRing(true);//是否响铃 $template->set_isVibrate(true);//是否震动 $template->set_isClearable(true);//通知栏是否可清除 // iOS推送需要设置的pushInfo字段 //$template ->set_pushInfo($actionLocKey,$badge,$message,$sound,$payload,$locKey,$locArgs,$launchImage); //$template ->set_pushInfo("test",1,"message","","","","",""); return $template; } // 透传消息模板 function IGtTransmissionTemplateDemo(){ $template = new \IGtTransmissionTemplate(); $template->set_appId($this->APPID);//应用appid $template->set_appkey($this->APPKEY);//应用appkey $template->set_transmissionType(1);//透传消息类型 $template->set_transmissionContent("测试离线");//透传内容 //iOS推送需要设置的pushInfo字段 //$template ->set_pushInfo($actionLocKey,$badge,$message,$sound,$payload,$locKey,$locArgs,$launchImage); //$template ->set_pushInfo("", 0, "", "", "", "", "", ""); return $template; }

}
?>