Heim  >  Artikel  >  Backend-Entwicklung  >  PHP Umeng-Nachrichten-Push-Klasse

PHP Umeng-Nachrichten-Push-Klasse

不言
不言Original
2018-05-31 17:16:165291Durchsuche

Dieser Artikel stellt hauptsächlich die PHP-Umeng-Nachrichten-Push-Kategorie vor, die einen gewissen Referenzwert hat. Jetzt können Freunde in Not darauf verweisen.

<?php
/**
 * 友盟消息 安卓 IOS 推送类
 * 
 */

class AlliesClass
{
    protected $host; //发送地址
    protected $app_key; //appkey
    protected $appMasterSecret; //app secret
    protected $description; //app secret

    //
    public function __construct($options = null)
    {
        if (is_array($options)){

            $this->host = &#39;https://msgapi.umeng.com/api/send&#39;;
            $this->app_key = $options[&#39;app_key&#39;];
            $this->appMasterSecret = $options[&#39;appMasterSecret&#39;];

            $this->description = "友盟接口推送";

        }else{
            return false;
        }

    }


    /**
     * @param $info
     * @param $device_token
     * @return mixed|string
     * 用户单播 和 列播
     */
    public function Android_Device_Push($info, $device_token)
    {
        $data[&#39;appkey&#39;] = $this->app_key;
        $data[&#39;timestamp&#39;] = time(); //时间戳

        if(is_array($device_token)){

            //批量用户列播
            $data[&#39;type&#39;] = &#39;listcast&#39;;
            $data[&#39;device_tokens&#39;] =  implode(&#39;,&#39;,$device_token); //数组转字符串

        }else{

            //一个用户单播
            $data[&#39;type&#39;] = &#39;unicast&#39;;
            $data[&#39;device_tokens&#39;] =  $device_token;
        }

        //payload内容
        $data[&#39;payload&#39;][&#39;display_type&#39;] = &#39;notification&#39;; //通知消息

        //payload body内容
        $data[&#39;payload&#39;][&#39;body&#39;][&#39;after_open&#39;] = "go_custom"; //后续操作打开app

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;ticker&#39;] = $info[&#39;ticker&#39;];
        $data[&#39;payload&#39;][&#39;body&#39;][&#39;title&#39;] = $info[&#39;title&#39;];
        $data[&#39;payload&#39;][&#39;body&#39;][&#39;text&#39;] = $info[&#39;title&#39;]; //广播通知不能为空补填

        //这里可以写附加字段
        $data[&#39;payload&#39;][&#39;extra&#39;][&#39;type&#39;] = $info[&#39;type&#39;];  //附加字段类型


        $data[&#39;production_mode&#39;] = $info[&#39;production_mode&#39;];

        $data[&#39;description&#39;] = $this->description;

        return $this->send($data, $this->host, $this->appMasterSecret);
    }

    /**
     * @param $info
     * @return mixed|string
     * 广播
     */
    public function Android_Broadcast($info)
    {
        $data[&#39;appkey&#39;] = $this->app_key;
        $data[&#39;timestamp&#39;] = time(); //时间戳

        //广播消息
        $data[&#39;type&#39;] = &#39;broadcast&#39;;

        //payload内容
        $data[&#39;payload&#39;][&#39;display_type&#39;] = &#39;notification&#39;; //通知消息

        //payload body内容
        $data[&#39;payload&#39;][&#39;body&#39;][&#39;after_open&#39;] = "go_custom"; //后续操作打开app

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;ticker&#39;] = $info[&#39;ticker&#39;];
        $data[&#39;payload&#39;][&#39;body&#39;][&#39;title&#39;] = $info[&#39;title&#39;];
        $data[&#39;payload&#39;][&#39;body&#39;][&#39;text&#39;] = $info[&#39;title&#39;]; //广播通知不能为空补填

        $data[&#39;payload&#39;][&#39;extra&#39;][&#39;type&#39;] = $info[&#39;type&#39;];  //附加字段类型1 跳转消息详情
        $data[&#39;payload&#39;][&#39;extra&#39;][&#39;prod_id&#39;] = $info[&#39;prod_id&#39;];  //附加字段消息详情id
        $data[&#39;payload&#39;][&#39;extra&#39;][&#39;text&#39;] = $info[&#39;text&#39;]; //

        $data[&#39;production_mode&#39;] = $info[&#39;production_mode&#39;];

        $data[&#39;description&#39;] = $this->description;

        return $this->send($data, $this->host, $this->appMasterSecret);
    }

    /**
     * @param $info
     * @param $device_token
     * @return mixed|string
     * 单播 和 列播
     */
    public function Ios_Device_Push($info, $device_token)
    {
        $data = array();

        $data[&#39;appkey&#39;] = $this->app_key;
        $data[&#39;timestamp&#39;] = time(); //时间戳

        if(is_array($device_token)){

            //批量用户列播
            $data[&#39;type&#39;] = &#39;listcast&#39;;
            $data[&#39;device_tokens&#39;] =  implode(&#39;,&#39;,$device_token); //数组转字符串

        }else{

            //一个用户单播
            $data[&#39;type&#39;] = &#39;unicast&#39;;
            $data[&#39;device_tokens&#39;] =  $device_token;
        }

        //payload内容
        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;alert&#39;] = $info[&#39;text&#39;]; //消息主体
        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;sound&#39;] = &#39;default&#39;; //声音

        $data[&#39;payload&#39;][&#39;type&#39;] = $info[&#39;type&#39;]; //消息类型 0打开消息详情

        $data[&#39;payload&#39;][&#39;prod_id&#39;] = $info[&#39;prod_id&#39;]; //消息id
        $data[&#39;payload&#39;][&#39;title&#39;] = $info[&#39;title&#39;];
        $data[&#39;payload&#39;][&#39;text&#39;] = $info[&#39;text&#39;]; //

        $data[&#39;production_mode&#39;] = $info[&#39;production_mode&#39;];

        $data[&#39;description&#39;] = $this->description;

        return $this->send($data, $this->host, $this->appMasterSecret);
    }


    public function Ios_Broadcast($info)
    {
        $data = array();

        $data[&#39;appkey&#39;] = $this->app_key;
        $data[&#39;timestamp&#39;] = time(); //时间戳

        //广播消息
        $data[&#39;type&#39;] = &#39;broadcast&#39;;

        //payload内容
        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;alert&#39;] = $info[&#39;title&#39;]; //消息主体
        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;sound&#39;] = &#39;chime&#39;; //声音
        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;badge&#39;] = 1; //显示角标

        $data[&#39;payload&#39;][&#39;type&#39;] = $info[&#39;type&#39;]; //消息类型 0打开消息详情

        $data[&#39;payload&#39;][&#39;prod_id&#39;] = $info[&#39;prod_id&#39;]; //消息id
        $data[&#39;payload&#39;][&#39;title&#39;] = $info[&#39;title&#39;];
        $data[&#39;payload&#39;][&#39;text&#39;] = $info[&#39;ticker&#39;]; //

        $data[&#39;production_mode&#39;] = $info[&#39;production_mode&#39;];

        $data[&#39;description&#39;] = $this->description;

        return $this->send($data, $this->host, $this->appMasterSecret);
    }

    /**
     * @param $data
     * @param $url_s
     * @param $appMasterSecret
     * @return mixed|string
     * curl 请求
     */
    private function send($data, $url_s, $appMasterSecret)
    {
        $postBody = json_encode($data);

        //加密
        $sign = md5("POST" . $url_s . $postBody . $appMasterSecret);
        $url = $url_s . "?sign=" . $sign;

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody );
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curlErrNo = curl_errno($ch);
        $curlErr = curl_error($ch);
        curl_close($ch);

        // print_r($result);
        // exit;

        if ($httpCode == "0") {
            // Time out
            return ("Curl error number:" . $curlErrNo . " , Curl error details:" . $curlErr . "\r\n");
        } else if ($httpCode != "200") {
            return ("Http code:" . $httpCode .  " details:" . $result . "\r\n");
        } else {
            return $result;
        }
    }
}

?>
rreee

Das Obige ist der gesamte Inhalt dieses Artikels Nun, ich danke Ihnen allen fürs Lesen. Weitere Informationen finden Sie auf der chinesischen PHP-Website!

Verwandte Empfehlungen:

PHP-Hypothekenberechnung

PHP-Screening-Methode zum Finden von Primzahlen

Das obige ist der detaillierte Inhalt vonPHP Umeng-Nachrichten-Push-Klasse. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP-HypothekenberechnungNächster Artikel:PHP-Hypothekenberechnung