前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >个推消息推送RestAPI版

个推消息推送RestAPI版

作者头像
素描
发布2019-09-19 10:43:08
1.5K0
发布2019-09-19 10:43:08
举报
文章被收录于专栏:编程录编程录

1.权鉴获取Token

代码语言:javascript
复制
 /**
     * 个推获取权鉴Token
     * @return bool
     */
    public static function getAuthToken()
    {
        require_once(__DIR__ . '/../../common/libs/requests/library/Requests.php');
        \Requests::register_autoloader();
        $headers = array(
            'Content-Type' => 'application/json'
        );
        $appID = Yii::$app->params['gt_app_id'];
        $appKey = Yii::$app->params['gt_app_key'];
        $timestamp = Func::getMsecTime();
        $masterSecret = Yii::$app->params['gt_master_secret'];
        $sign = hash('sha256',$appKey.$timestamp.$masterSecret);
        $data = [
            'sign' => $sign,
            'timestamp' => $timestamp,
            'appkey' => $appKey
        ];
        $jsonData = json_encode($data);
        $postUrl = 'https://restapi.getui.com/v1/'.$appID.'/auth_sign';
        $response = \Requests::post($postUrl, $headers, $jsonData);
        $postData = json_decode($response->body);
        if($postData->result == 'ok'){
            return $postData->auth_token;
        }else{
            return false;
        }
    }

2.向客户端推送消息

代码语言:javascript
复制
/**
     * 向客户端推送消息
     * @param $title
     * @param $content
     * @return bool
     */
    public static function pushMessage($title,$content)
    {
        require_once(__DIR__ . '/../../common/libs/requests/library/Requests.php');
        \Requests::register_autoloader();
        $authToken = Func::getAuthToken();
        $headers = array(
            'Content-Type' => 'application/json',
            'authtoken' => $authToken
        );
        $data = [
            'message' => [
                'appkey' => Yii::$app->params['gt_app_key'],
                'is_offline' => false,
                'msgtype' => 'notification',
            ],
            //参数:link notification
            'notification' => [
                'style' => [
                    'type' => 0,
                    'text' => $content,
                    'title' => $title,
                    'logourl' => 'http://bazhua.igexin.com/file/201908/upload_fe428cf5b8a24e68b3bd9aa2850ef37b.png'
                ],
                //'url' => 'http://www.baidu.com',
            ],
            'requestid' => Func::GenSecret(20,1)
        ];
        $jsontData = json_encode($data);
        $response = \Requests::post('https://restapi.getui.com/v1/'.Yii::$app->params['gt_app_id'].'/push_app', $headers, $jsontData);
        $postData = json_decode($response->body);
        if($postData->result == 'ok'){
            return true;
        }else{
            return false;
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档