Laravel-OPPO推送

Laravel - OPPO推送

代码如下(示例):

/**
     * 广播推送消息
     */
    /**
     * @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=[]){

        $url = 'https://api.push.oppomobile.com';
        $header=[];
        $auth_token=self::getToken();
        // 保存消息内容体,获取消息Id
        $postBody = [
            'auth_token'=>$auth_token,
            'style'=>1, // 通知栏样式 1. 标准样式 2. 长文本样式 3. 大图样式
            'title'=>$title, // 设置在通知栏展示的通知栏标题, 【字数限制1~50,中英文均以一个计算】
//            'sub_title'=>'花狸小说推送', // 子标题 设置在通知栏展示的通知栏标题, 【字数限制1~10,中英文均以一个计算】
            // 设置在通知栏展示的通知的内容,【必填,
            //标准样式(style 为 1)字数限制200以内(兼容API文档以前定义,实际手机端通知栏消息只能展示50字数),
            //长文本样式(style 为 2)限制128个以内,大图样式
            //(style 为 3)字数限制50以内,中英文均以一个计算】
            'content'=>$content,
            // 点击动作类型0,启动应用;1,打开应用内页(activity的intent action);2,打开网页;
            //4,打开应用内页(activity);【非必填,默认值为0】;5,Intent scheme URL
            'click_action_type'=>5,
//            'click_action_activity'=>'com.nearme.instant.action.PUSH', // 应用内页地址【click_action_type为1/4/
            'click_action_url'=>$page_param, // 网页地址或【click_action_type为2与5时必填,长度500】
//            'action_parameters'=>'{"page":"'.$page_param.'"}', // 动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"}
//            'show_time_type'=>0, // 展示类型 (0, “即时”),(1, “定时”)
//            'show_start_time'=>0,// 定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数
//            'show_end_time'=>0, // 定时展示结束时间(根据time_zone转换成当地时间),时间的毫秒数
//            'push_time_type'=>0, // 定时推送 (0, “即时”),(1, “定时”), 【只对全部用户推送生效】
//            'push_start_time'=>0, // 定时推送开始时间(根据time_zone转换成当地时间), 【push_time_type 为1必填】,时间的毫秒数
        ];
        $response_message = self::curl_send($url.'/server/v1/message/notification/save_message_content', $postBody,$header);
        $response_message = json_decode($response_message,true);
        $message_id = isset($response_message['data']['message_id']) ? $response_message['data']['message_id'] : '';
        if(empty($message_id)){
            return false;
        }
        // 发送通知栏消息
        $postBody = [
            'auth_token'=>$auth_token,
            'message_id'=>$message_id, // 消息Id
            'target_type'=>1, // 目标类型, 1:ALL; 2:registration_id; 5:别名 6:标签
//            'target_value'=>'CN_663376bb52721f75786432241d057fa2'
        ];
        if($target_value){
            $postBody['target_type'] = 2;
            $postBody['target_value'] = implode(";",$target_value);

        }
        $response_broadcast = self::curl_send($url.'/server/v1/message/notification/broadcast', $postBody,$header);
        $response_broadcast = json_decode($response_broadcast,true);
        if(isset($response_broadcast['data']['10000'])){
            return false;
        }
        return $response_broadcast;
    }

获取auth_token

代码如下(示例):

function getToken(){
        $key="OPPO_TOKEN";
        $token = Cache::get($key);
        if(empty($token)){
            $url = 'https://api.push.oppomobile.com';
            $postUrl = $url."/server/v1/auth";
            $timestamp = self::millTimestamp();
            $postBody = [
                'app_key'=>self::$config['AppKey'],
                'timestamp'=>$timestamp,
            ];
            $masterSecret = self::$config['MasterSecret'];
            $sign = $postBody['app_key'].$timestamp.$masterSecret;
            $postBody['sign'] = hash("sha256", $sign); // sha256(appkey+timestamp+mastersecret) mastersecret为注册应用时生成
            $header=[];

            // 获取开发者身份鉴权
            $response = self::curl_send($postUrl, $postBody,$header);
            $response = json_decode($response,true);
            $auth_token = isset($response['data']['auth_token']) ? $response['data']['auth_token'] :''; // 权限令牌,推送消息时,需要提供auth_token,有效期默认为24小时,过期后无法使用
            if(empty($auth_token)){
                return false;
            }else{
                return $auth_token;
            }
        }
    }
    /**
     * 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);
        curl_close($ch);
        return $result;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值