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);

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM