ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

oppo推送

2021-12-14 16:35:18  阅读:181  来源: 互联网

标签:ch auth response time curl 推送 message oppo


转载于:https://blog.csdn.net/qq_23564667/article/details/108662377

/**
 * @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_oppo($title='推荐书籍',$content='',$page_param='/Detail?bid=51011',$target_value=['CN_663376bb52721f75786432241d057fa2']){
    if(empty($content)){
        $content = '她多次求来的结缘机会,被他一点点消耗殆尽,直到因果台上姓名消...';
    }

    $url = 'https://api.push.oppomobile.com';
    $postUrl = $url."/server/v1/auth";
    $timestamp = $this->millTimestamp();
    $postBody = [
        'app_key'=>'ae2*****be286d',
        'timestamp'=>$timestamp,
    ];
    $masterSecret = 'd51fa7b********3c14a21';
    $sign = $postBody['app_key'].$timestamp.$masterSecret;
    $postBody['sign'] = hash("sha256", $sign); // sha256(appkey+timestamp+mastersecret) mastersecret为注册应用时生成
    $header=[];

    // 获取开发者身份鉴权
    $response = $this->curl_send($postUrl, $postBody,$header);
    $response = json_decode($response,true);
    $auth_token = isset($response['data']['auth_token']) ? $response['data']['auth_token'] :''; // 权限令牌,推送消息时,需要提供auth_token,有效期默认为24小时,过期后无法使用
    $create_time = isset($response['data']['create_time']) ? $response['data']['create_time'] : 0; // 过期时间时间毫秒数
    if(empty($auth_token)){
        json_back(-1, '获取开发者身份鉴权失败!');
    }

    // 保存消息内容体,获取消息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'=>1,
        'click_action_activity'=>'com.nearme.instant.action.PUSH', // 应用内页地址【click_action_type为1/4/
//            'click_action_url'=>'com.qr.huali://Detail?bid=51011', // 网页地址或【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 = $this->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)){
        json_back(-1, '保存消息内容体,获取消息Id失败!');
    }
    // 发送通知栏消息
    $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 = $this->curl_send($url.'/server/v1/message/notification/broadcast', $postBody,$header);
    $response_broadcast = json_decode($response_broadcast,true);
    if(isset($response_broadcast['data']['10000'])){
        json_back(-1, '发送失败,有可能是registration_id格式不正确!', $response_broadcast);
    }
 json_back(1, '发送成功!', $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;
}

  

标签:ch,auth,response,time,curl,推送,message,oppo
来源: https://www.cnblogs.com/zinging/p/15688544.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有