123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- class OppoPush{
- private static $appkey;
- private static $mastersecret;
- private static $auth_token_url = 'https://api.push.oppomobile.com/server/v1/auth';
- private static $save_message_url = 'https://api.push.oppomobile.com/server/v1/message/notification/save_message_content';
- private static $push_all_url = 'https://api.push.oppomobile.com/server/v1/message/notification/broadcast';
- private static $push_one_url = 'https://api.push.oppomobile.com/server/v1/message/notification/unicast';
- # 安卓初始参数配置
- public static function _init($desc){
- self::$appkey = $desc['appkey'];
- self::$mastersecret = $desc['mastersecret'];
- }
-
- # 安卓推所有(push_time_type:1 定时,0即时)
- public static function pushAllAndroid($desc){
- //参数:app_key timestamp sign:sha256(appkey+timestamp+mastersecret)
- self::_init($desc);
- $auth_token = self::get_auth_token();
- if( !$auth_token ) return false;
- $message_id = self::save_message_content($desc, $auth_token);
- if( !$message_id ) return false;
- $data = array();
- $data['auth_token'] = $auth_token;
- $data['message_id'] = $message_id;
- $data['target_type'] = 1;
- //$data['target_value'] = '';
- $str = '?';
- foreach($data as $k=>$v){
- $str .= $k.'='.$v.'&';
- }
- $str = rtrim($str, '&');
- $url = self::$push_all_url.$str;
- $result = self::curl_get($url, $data);
- return $result;
- }
- # 安卓推单个
- public static function pushPartAndroid($desc){
- //参数:app_key timestamp sign:sha256(appkey+timestamp+mastersecret)
- self::_init($desc);
- $auth_token = self::get_auth_token();
- $data = array();
- $data['target_type'] = 2;
- $data['target_value'] = $desc['reg_id'];
- $content = self::push_data_format($desc);
- $data['notification'] = $content;
- $post = array();
- $post['message'] = json_encode($data);
- $post['auth_token'] = $auth_token;
- $str = '?';
- foreach($post as $k=>$v){
- $str .= $k.'='.$v.'&';
- }
- $str = rtrim($str, '&');
- $url = self::$push_one_url.$str;
- //$data['target_value'] = '';
- $result = self::curl_get($url, $post);
- return $result;
- }
- public static function get_auth_token(){
- $data = array();
- $data['app_key'] = self::$appkey;
- $data['timestamp'] = time()*1000;
- $str = $data['app_key'].$data['timestamp'].self::$mastersecret;
- $data['sign'] = self::sha256($str);
- $url = self::$auth_token_url.'?app_key='.$data['app_key'].'×tamp='.$data['timestamp'].'&sign='.$data['sign'];
- $result = self::curl_get($url, $data);
- if( $result['code'] == 0 && isset($result['data']['auth_token']) ){
- return $result['data']['auth_token'];
- }else{
- return false;
- }
- }
- # 保存消息体
- public static function save_message_content($desc,$auth_token){
- $data = self::push_data_format( $desc );
- $data['auth_token'] = $auth_token;
- $str = '?';
- foreach($data as $k=>$v){
- $str .= $k.'='.urlencode($v).'&';
- }
- $str = rtrim($str, '&');
- $url = self::$save_message_url.$str;
- $result = self::curl_get($url, $data);
- if( $result['code'] == 0 && isset($result['data']['message_id']) ){
- return $result['data']['message_id'];
- }else{
- return false;
- }
- }
- # 拼推送基本参数
- public static function push_data_format($desc){
- $data = array();
- $time = time();
- $data['app_message_id'] = $desc['app'].$time.rand(100,999);
- $data['title'] = $desc['title'];
- $data['sub_title'] = isset($desc['sub_title'])? $desc['sub_title'] : '';
- $data['content'] = $desc['message'];
- $data['click_action_type'] = isset($desc['click_action_type'])? $desc['click_action_type'] : 0; //点击动作类型0,启动应用;1,打开应用内页(activity的intent action);2,打开网页;4,打开应用内页(activity);【非必填,默认值为0】;5,Intent scheme URL
- //$data['click_action_activity'] = isset($desc['click_action_activity']) ? $desc['click_action_activity'] : ''; //click_action_type为1时这里填写com.coloros.push.demo.internal click_action_type为4时这里填写com.coloros.push.demo.component.InternalActivity
- //$data['click_action_url'] = isset($desc['click_action_url']) ? $desc['click_action_url'] : ''; //网页地址【click_action_type为2必填,长度500】
- //$data['action_parameters'] = isset($desc['action_parameters']) ? $desc['action_parameters'] : ''; //动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"}
- $data['off_line_ttl'] = isset($desc['off_line_ttl'])? $desc['off_line_ttl'] : 36000; //离线消息的存活时间(time_to_live) (单位:秒), 【off_line值为true时,必填,最长3天】
- $data['push_time_type'] = 0;//isset($desc['push_time_type'])? $desc['push_time_type'] : 1; //定时推送 (0, “即时”),(1, “定时”), 【只对全部用户推送生效】
- $data['push_start_time'] = isset($desc['push_start_time'])? $desc['push_start_time']+1000 : $time*1000; //定时推送开始时间(根据time_zone转换成当地时间), 【push_time_type 为1必填】,时间的毫秒数
- $data['show_time_type'] = 1; //展示类型 (0, “即时”),(1, “定时”)
- $data['show_start_time'] = isset($desc['show_start_time'])? $desc['show_start_time'] +2000: $time*1000;//定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数
- $data['show_end_time'] = isset($desc['show_start_time'])? $desc['show_start_time']+ 3000: ($time+3)*1000;//定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数
- $data['off_line'] = true;
- return $data;
- }
- /* PHP sha256() */
- public static function sha256($data, $rawOutput=false){
- if(!is_scalar($data)){
- return false;
- }
- $data = (string)$data;
- $rawOutput = !!$rawOutput;
- return hash('sha256', $data, $rawOutput);
- }
- public static function curl_get($url, $param){
- if(isset($param['auth_token']) ){
- $auth_token = $param['auth_token'];
- }else{
- $auth_token = '';
- }
- $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token);
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);//
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- //curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- //curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return json_decode($data,true);
- }
- public static function curl_post($url, $param){
- $auth_token = isset($param['auth_token'])? $param['auth_token'] : '';
- unset($param['auth_token']);
- $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token);
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);//
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return json_decode($data,true);
- }
- }
|