PHP 友盟消息推送类

阅读量:23
2021-04-17

这篇文章主要介绍了关于PHP 友盟消息推送类,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

<?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 = '';
            $this->app_key = $options['app_key'];
            $this->appMasterSecret = $options['appMasterSecret'];

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

        }else{
            return false;
        }

    }


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

        if(is_array($device_token)){

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

        }else{

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

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

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

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

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


        $data['production_mode'] = $info['production_mode'];

        $data['description'] = $this->description;

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

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

        //广播消息
        $data['type'] = 'broadcast';

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

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

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

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

        $data['production_mode'] = $info['production_mode'];

        $data['description'] = $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['appkey'] = $this->app_key;
        $data['timestamp'] = time(); //时间戳

        if(is_array($device_token)){

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

        }else{

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

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

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

        $data['payload']['prod_id'] = $info['prod_id']; //消息id
        $data['payload']['title'] = $info['title'];
        $data['payload']['text'] = $info['text']; //

        $data['production_mode'] = $info['production_mode'];

        $data['description'] = $this->description;

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


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

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

        //广播消息
        $data['type'] = 'broadcast';

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

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

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

        $data['production_mode'] = $info['production_mode'];

        $data['description'] = $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);
        $ = curl_getinfo($ch, CURLINFO_);
        $curlErrNo = curl_errno($ch);
        $curlErr = curl_error($ch);
        curl_close($ch);

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

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

?>
 <?php
    //使用类
    $options['app_key'] = ''; //友盟key;
    $options['appMasterSecret'] = ''; //友盟MasterSecret

    $this->umeng = new AlliesClass($options);

    $info['ticker'] = '';
    $info['title'] ='';
    $info['title'] =''; //广播通知不能为空补填
    $info['type'] = 4;  //附加字段类型1 跳转消息详情
    $info['text'] = '提醒'; //
    $info['production_mode'] = 'false';

    $value=$this->umeng->Android_Device_Push($info, $token);  //单播传入数据和用户token

    $value=$this->umeng->Android_Broadcast($info);  //广播直接传入数据
?>

以上就是本篇文章的全部内容了,感谢大家阅读。更多请关注星网无限!

相关推荐:

PHP房贷计算

PHP筛选法求素数

以上就是PHP 友盟消息推送类的详细内容,更多请关注星网无限其它相关文章!

声明:本文原创发布星网无限,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@处理

THE END

发表评论

相关推荐

  • 有关fgets()函数的文章推荐10篇

    以下正文:这篇文章主要介绍了PHP文件读写操作相关函数总结,本文总结了fwrite()、fread()、fgets()、fgetc()、file()、readf ...

    阅读量:65
    2021-04-19
  • php递归与无限分类实例详解

    这篇文章主要介绍了php实现递归与无限分类的方法,涉及php的递归操作技巧,需要的朋友可以参考下本文实例讲述了php实现递归与 ...

    阅读量:45
    2021-04-19
  • CI框架中zip类的使用

    CI框架自带的zip类简单实用,本文就来简单说一下ci框架的zip类的使用,需要的朋友可以参考下CI框架自带的zip类简单实用,本文 ...

    阅读量:44
    2021-04-18
  • SWFUpload插件上传文件的代码

    这篇文章主要介绍了文件上传之SWFUpload插件(代码),实现此代码主要分为两部分:1.前台文件和 2.后台文件upload.php,需要 ...

    阅读量:42
    2021-04-18
  • 关于destoon的URL Rewrite设置方法

    这篇文章主要介绍了destoon的URL Rewrite(伪静态)设置方法,需要的朋友可以参考下1、如果您的服务器支持.htaccess,则无需设 ...

    阅读量:47
    2021-04-18