Laravel-vivo推送

提示:以下是本篇文章正文内容,下面案例可供参考

代码如下(示例):

/**
     * 广播推送消息
     */
    /**
     * @param string $title 标题
     * @param string $content 描述
     * @param string $page_param 内页地址
     * @param array $target_value 指定推送 registration_id
     * @Date 2020/9/18 10:59
     * @Author wzb
     */
    public static function push($title='',$content='',$page_param='',$target_value=[]){

        $auth_token=self::getToken();
        $url = "https://api-push.vivo.com.cn/message/saveListPayload";
        $post['alias'] = '';
        $post['notifyType'] = 2;
        $post['title'] = $title;
        $post['content'] =$content;
        $post['skipType'] = 4;
        $post['skipContent'] = $page_param;
        $post['requestId'] = self::$config['AppSecret'];
        $post_string = json_encode($post);
        $res = self::RequestCurl($url, $post_string,$auth_token);
        $response_message = json_decode($res,true);
        $taskId = isset($response_message['taskId']) ? $response_message['taskId'] : '';
        if(empty($taskId)){
            return false;
        }
        // 发送通知栏消息
        $postBody = [
            'taskId'=>$taskId, // 消息Id
            'regIds'=>$target_value,// regId列表 个数大于等于2,小于等于1000,regId长度23个字符(regIds,aliases   两者需一个不为空,两个不为空,取regIds)
            'requestId'=>self::$config['AppSecret'],// 请求唯一标识,最大64字符
            'pushMode'=>0,// 推送模式 0:正式推送;1:测试推送,不填默认为0(测试推送,只能给web界面录入的测试用户推送;审核中应用,只能用测试推送)
        ];

        $url = "https://api-push.vivo.com.cn/message/pushToList";
        $post_string = json_encode($postBody);
        $response_broadcast = self::RequestCurl($url, $post_string,$auth_token);;
        $response_broadcast = json_decode($response_broadcast,true);
        if(isset($response_broadcast['data']['10000'])){
            return false;
        }
        return $response_broadcast;
    }
    //单推
    public static function send($title='',$content='',$page_param='',$regId=''){
        $auth_token=self::getToken();
        $url = "https://api-push.vivo.com.cn/message/send";
        $post['regId'] = $regId;
        $post['alias'] = '';
        $post['notifyType'] = 2;
        $post['title'] = $title;
        $post['content'] =$content;
        $post['skipType'] = 4;
        $post['skipContent'] = $page_param;
        $post['requestId'] = self::$config['AppSecret'];
        $post_string = json_encode($post);
        $res = self::RequestCurl($url, $post_string,$auth_token);
        $response_broadcast = json_decode($res,true);
        return $response_broadcast;
    }
    

2.获取authToken

代码如下(示例):

	function getToken(){
        $url = 'https://api-push.vivo.com.cn';
        $key="VIVO_TOKEN";
        $token = Cache::get($key);
        if(empty($token)){
            $postUrl = $url."/message/auth";
            $timestamp = self::millTimestamp();
            $postBody = [
                'appId'=>self::$config['AppID'],
                'appKey'=>self::$config['AppKey'],
                'timestamp'=>$timestamp,
            ];
            $appSecret = self::$config['AppSecret'];
            $sign = $postBody['appId'].$postBody['appKey'].$timestamp.$appSecret;
            $postBody['sign'] = strtolower(md5($sign)); // 签名 使用MD5算法,字符串trim后拼接(appId+appKey+timestamp+appSecret),然后通过MD5加密得到的值(字母小写)

            $response = self::RequestCurl($postUrl, json_encode($postBody));
            $response = json_decode($response,true);
            // 当鉴权成功时才会有该字段,推送消息时,需要提供authToken,有效期默认为1天,过期后无法使用。一个appId可对应多个token,24小时过期,业务方做中心缓存,1-2小时更新一次。
            $auth_token = isset($response['authToken']) ? $response['authToken'] :'';
            if(empty($auth_token)){
                return false;
            }else{
                Cache::put($key,$auth_token,7200);
                return $auth_token;
            }
        }
        return $token;
    }
    /**
     * 13位毫妙时间戳
     * @return string
     */
    function millTimestamp(){
        list($usec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($usec) + floatval($sec)) * 1000);
    }
    /**
     * viVo请求发送
     * @param $remote_server
     * @param $post_string
     * @param string $authToken
     * @return mixed
     */
    function RequestCurl($remote_server, $post_string, $authToken = "")
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        //根据情况展示
        if ($authToken !== "") {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'authToken:' . $authToken,
                'Content-Type: application/json;charset=utf-8'
            ));
        } else {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json;charset=utf-8'
            ));
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

Curl返回:

{
	"result": 0,
	"desc": "请求成功",
	"taskId": "986310665445650948"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值