個推 服務端 SDK

<?php
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 ("<br><br>");

}
//所有推送接口均支持四個消息模板,依次爲通知彈框下載模板,通知鏈接模板,通知透傳模板,透傳模板
//注: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;
}

}
?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章