• VIVO 推送封装函数


    转自:https://blog.csdn.net/qq_23564667/article/details/108684134

    /**
     * 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
     */
    function send_push_message_vivo($title='推荐书籍',$content='',$page_param='/Detail?bid=51011',$target_value=['16005080910330978890938','12345678901234567890122']){
        if(empty($content)){
            $content = '她多次求来的结缘机会,被他一点点消耗殆尽,直到因果台上姓名消...';
        }
    
        $url = 'https://api-push.vivo.com.cn';
        $postUrl = $url."/message/auth";
        $timestamp = $this->millTimestamp();
        $postBody = [
            'appId'=>'1***8',
            'appKey'=>'7c52************068***8193',
            'timestamp'=>$timestamp,
        ];
        $appSecret = '8b85***************12df8';
        $sign = $postBody['appId'].$postBody['appKey'].$timestamp.$appSecret;
        $postBody['sign'] = strtolower(md5($sign)); // 签名 使用MD5算法,字符串trim后拼接(appId+appKey+timestamp+appSecret),然后通过MD5加密得到的值(字母小写)
    
        // 获取开发者身份鉴权
        $header[] = "Content-type: application/json";
        $response = $this->curl_send($postUrl, json_encode($postBody),$header);
        $response = json_decode($response,true);
        // 当鉴权成功时才会有该字段,推送消息时,需要提供authToken,有效期默认为1天,过期后无法使用。一个appId可对应多个token,24小时过期,业务方做中心缓存,1-2小时更新一次。
        $auth_token = isset($response['authToken']) ? $response['authToken'] :'';
        if(empty($auth_token)){
            json_back(-1, '获取开发者身份鉴权失败!');
        }
    
        $header[] = "authToken:{$auth_token}";
        // 保存消息内容体,获取消息Id
        $postBody = [
            'notifyType'=>4, // 通知类型 1:无,2:响铃,3:振动,4:响铃和振动
            'title'=>$title, //通知标题(用于通知栏消息) 最大20个汉字(一个汉字等于两个英文字符,即最大不超过40个英文字符)
            'content'=>$content, // 通知内容(用于通知栏消息) 最大50个汉字(一个汉字等于两个英文字符,即最大不超过100个英文字符)
    //            'timeToLive'=>1, // 消息保留时长 单位:秒,取值至少900秒,最长7天。当值为空时,默认一天。
            'skipType'=>1, // 点击跳转类型 1:打开APP首页 2:打开链接 3:自定义 4:打开app内指定页面
            // 跳转内容 跳转类型为2时,跳转内容最大1000个字符,跳转类型为3或4时,跳转内容最大1024个字符,
            //skipType传3需要在onNotificationMessageClicked回调函数中自己写处理逻辑。关于skipContent的内容可以参考【vivo推送常见问题汇总】
            'skipContent'=>'',
            'classification'=>0, // 消息类型 0:运营类消息,1:系统类消息。不填默认为0
            'requestId'=>$appSecret, // 用户请求唯一标识 最大 64 字符 appSecret
    
        ];
        $response_message = $this->curl_send($url.'/message/saveListPayload', json_encode($postBody),$header);
        $response_message = json_decode($response_message,true);
        $taskId = isset($response_message['taskId']) ? $response_message['taskId'] : '';
        if(empty($taskId)){
            json_back(-1, '保存消息内容体,获取消息Id失败!');
        }
        // 发送通知栏消息
        $postBody = [
            'taskId'=>$taskId, // 消息Id
            'regIds'=>$target_value,// regId列表 个数大于等于2,小于等于1000,regId长度23个字符(regIds,aliases   两者需一个不为空,两个不为空,取regIds)
            'requestId'=>$appSecret,// 请求唯一标识,最大64字符
            'pushMode'=>0,// 推送模式 0:正式推送;1:测试推送,不填默认为0(测试推送,只能给web界面录入的测试用户推送;审核中应用,只能用测试推送)
        ];
    
        $response_broadcast = $this->curl_send($url.'/message/pushToList', json_encode($postBody),$header);
        dump($postBody);
        dump($response_broadcast);
        $response_broadcast = json_decode($response_broadcast,true);
        if(isset($response_broadcast['data']['10000'])){
            json_back(-1, '发送失败,有可能是registration_id格式不正确!', $response_broadcast);
        }
        return $response_broadcast;
    }

    需要用的函数

    /**
     * 13位毫妙时间戳
     * @return string
     */
    function millTimestamp(){
        list($usec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($usec) + floatval($sec)) * 1000);
    }
    
    /**
     * curl post请求
     * @param string $url 地址
     * @param string $postData 数据
     * @param array $header 头部
     * @return bool|string
     * @Date 2020/9/17 17:12
     * @Author wzb
     */
    public static function curl_send($url='',$postData='',$header=[]){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5000);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
    
        if($header){
            curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        }
    
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curlErrNo = curl_errno($ch);
        $curlErr = curl_error($ch);
        curl_close($ch);
        return $result;
    }
  • 相关阅读:
    Best HTTP
    Unity3D游戏轻量级xlua热修复框架
    线段树
    7.1
    BZOJ 3011: [Usaco2012 Dec]Running Away From the Barn( dfs序 + 主席树 )
    BZOJ 3585: mex( 离线 + 线段树 )
    2015暑假集训
    BZOJ 3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛( dp )
    BZOJ 2693: jzptab( 莫比乌斯反演 )
    2015.7.31
  • 原文地址:https://www.cnblogs.com/zinging/p/15636584.html
Copyright © 2020-2023  润新知