極光推送

<?php
class client
{
    private $appKey;
    private $masterSecret;
    private $retryTimes;
    private $logFile;
    private static $zones = [
            'push' => 'https://api.jpush.cn/v3/',
            'report' => 'https://report.jpush.cn/v3/',
            'device' => 'https://device.jpush.cn/v3/devices/',
            'alias' => 'https://device.jpush.cn/v3/aliases/',
            'tag' => 'https://device.jpush.cn/v3/tags/',
            'schedule' => 'https://api.jpush.cn/v3/schedules/'
    ];

    public function __construct($appKey,$masterSecret)
    {
        $this->appKey = $appKey;
        $this->masterSecret = $masterSecret;
    }
    public function push() { return new PushPayload($this); }
    public function getAuthStr() { return $this->appKey . ":" . $this->masterSecret; }
    public function makeURL($key) {
        return self::$zones[$key];
    }

    public function push_res_msg($res_arr)
    {
        if(isset($res_arr['error'])){                       //若是返回了error則證實失敗        
            $error_code=$res_arr['error']['code'];             //錯誤碼
            switch ($error_code) {
                case 200:
                    $message= ['error'=>1,'msg'=>'發送成功!'];
                    break;
                case 1000:
                    $message= ['error'=>1,'msg'=>'失敗(系統內部錯誤)'];
                    break;
                case 1001:
                    $message =['error'=>1,'msg'=>'失敗(只支持 HTTP Post 方法,不支持 Get 方法)'] ;
                    break;
                case 1002:
                    $message= ['error'=>1,'msg'=>'失敗(缺乏了必須的參數)'];
                    break;
                case 1003:
                    $message= ['error'=>1,'msg'=>'失敗(參數值不合法)'];
                    break;
                case 1004:
                    $message= ['error'=>1,'msg'=>'失敗(驗證失敗)'];
                    break;
                case 1005:
                    $message= ['error'=>1,'msg'=>'失敗(消息體太大)'] ;
                    break;
                case 1008:
                    $message= ['error'=>1,'msg'=>'失敗(appkey參數非法)'];
                    break;
                case 1020:
                    $message= ['error'=>1,'msg'=>'失敗(只支持 HTTPS 請求)'];
                    break;
                case 1030:
                    $message= ['error'=>1,'msg'=>'失敗(內部服務超時)'] ;
                    break;
                default:
                    $message= ['error'=>1,'msg'=>'失敗(返回其餘狀態,目前不清楚額,請聯繫開發人員!)'];
                    break;
            }
        }else{
            $message=['error'=>0,'msg'=>"發送成功!"];
        }
        return $message;
    }

}

/**
* 
*/
class PushPayload
{

    private $client;
    private $url;
    private $message;
    private $notification;
    private $options;
    function __construct($client)
    {
        $this->client = $client;
        $this->url = $this->client->makeURL('push').'push';
    }

    public function platform($platform)
    {
         return $this->updateAudience('platform', $platform, 'platform');
    }

    public function audience($audience)
    {
         return $this->updateAudience('audience', $audience, 'audience');
    }

     public function options($m_time)
    {
        if($m_time){
            $options = array();
            $options["sendno"]=time();
            $options["time_to_live"]=$m_time; //保存離線時間的秒數默認爲一天
            $options["apns_production"]=false;
            $this->options = $options;
        }
        return $this;
    }

    public function build()
    {
        $payload = array();
        if (is_null($this->platform)) {
            return ("platform must be set");
        }
        $payload["platform"] = $this->platform;

        if (is_null($this->audience)) {
            return ("audience must be set");
        }
        $payload["audience"] = $this->audience;

         if (is_null($this->notification)) {
            return ("notification must be set");
        }
        $payload["notification"] = $this->notification;

         if (is_null($this->notification)) {
            return ("message must be set");
        }
        $payload["message"] = $this->message;

         if (is_null($this->options)) {
            return ("options must be set");
        }
        $payload["options"] = $this->options;
        return $payload;
    }

     public function send()
    {
        return Http::post($this->client, $this->url, $this->build());  
    }

    public function notification($content,array $msg = array())
    {
            $notification = array();
            if(is_string($content)){
                $notification['alert'] = $content;
                //安卓自定義
                if(!empty($msg)){
                    $notification["android"]=array(
                        "alert"=>$content,
                        "title"=>"",
                        "builder_id"=>1,
                        "extras"=>array("type"=>$msg['type'], "txt"=>$msg['txt'])
                    );
                    //ios的自定義
                    $notification["ios"]=array(
                        "alert"=>$content,
                        "badge"=>"1",
                        "sound"=>"default",
                        "extras"=>array("type"=>$msg['type'], "txt"=>$msg['txt'])
                    );
                }
                $this->notification = $notification;
            }
            return $this;
    }



    public function message($msg_content,array $msg = array())
    {
        if(is_string($msg_content)){
            $message = array();
            $message['msg_content'] = $msg_content;
            if(!empty($msg)){
                $message['extras'] = array("type"=>$msg['type'], "txt"=>$msg['txt']);
            }
            $this->message = $message;
        }
        return $this;
    }

    private function updateAudience($key, $value, $name) 
    {
        if (@is_null($this->$key)) {
            $this->$key = array();
        }
        if ($value) {
           $this->$key = $value;
        } else {
            return ("Invalid $name value");
        }
        return $this;
    }


}

class http
{
    static function post ($client,$postUrl,$param)
    {   
        $base64=base64_encode($client->getAuthStr());
        $header=array("Authorization:Basic $base64","Content-Type:application/json");
        if (empty($param)) { return false; }

        $curlPost = json_encode($param);
        $ch = curl_init();                                      //初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);                 //抓取指定網頁
        curl_setopt($ch, CURLOPT_HEADER, 0);                    //設置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求結果爲字符串且輸出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);           // 增長 HTTP Header(頭)裏的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 終止從服務端進行驗證
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);                                 //運行curl
        curl_close($ch);
        return $client->push_res_msg(json_decode($data, true));
    }



}

調用php

            $content = array('tag'=>array($userinfo['tag']));//標籤
                    //$content = 'all';//標籤
                    $alert = option('config.push_alert');
                    $extras = ['type'=>1,'txt'=>'https://mall.epaikj.com/wap/'];
                    $m_time = 600;
                    import('PushClient');
                    $client =  new client(option('config.push_app_key'),option('config.push_secret'));
                    $res = $client->push()
                            ->platform('all')
                            ->audience($content)
                            ->notification($alert,$extras)
                            ->message($alert,$extras)
                            ->options($m_time)
                            ->send();  
相關文章
相關標籤/搜索