Geen omschrijving

OppoPush.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. class OppoPush{
  3. private static $appkey;
  4. private static $mastersecret;
  5. private static $auth_token_url = 'https://api.push.oppomobile.com/server/v1/auth';
  6. private static $save_message_url = 'https://api.push.oppomobile.com/server/v1/message/notification/save_message_content';
  7. private static $push_all_url = 'https://api.push.oppomobile.com/server/v1/message/notification/broadcast';
  8. private static $push_one_url = 'https://api.push.oppomobile.com/server/v1/message/notification/unicast';
  9. # 安卓初始参数配置
  10. public static function _init($desc){
  11. self::$appkey = $desc['appkey'];
  12. self::$mastersecret = $desc['mastersecret'];
  13. }
  14. # 安卓推所有(push_time_type:1 定时,0即时)
  15. public static function pushAllAndroid($desc){
  16. //参数:app_key timestamp sign:sha256(appkey+timestamp+mastersecret)
  17. self::_init($desc);
  18. $auth_token = self::get_auth_token();
  19. if( !$auth_token ) return false;
  20. $message_id = self::save_message_content($desc, $auth_token);
  21. if( !$message_id ) return false;
  22. $data = array();
  23. $data['auth_token'] = $auth_token;
  24. $data['message_id'] = $message_id;
  25. $data['target_type'] = 1;
  26. //$data['target_value'] = '';
  27. $str = '?';
  28. foreach($data as $k=>$v){
  29. $str .= $k.'='.$v.'&';
  30. }
  31. $str = rtrim($str, '&');
  32. $url = self::$push_all_url.$str;
  33. $result = self::curl_get($url, $data);
  34. return $result;
  35. }
  36. # 安卓推单个
  37. public static function pushPartAndroid($desc){
  38. //参数:app_key timestamp sign:sha256(appkey+timestamp+mastersecret)
  39. self::_init($desc);
  40. $auth_token = self::get_auth_token();
  41. $data = array();
  42. $data['target_type'] = 2;
  43. $data['target_value'] = $desc['reg_id'];
  44. $content = self::push_data_format($desc);
  45. $data['notification'] = $content;
  46. $post = array();
  47. $post['message'] = json_encode($data);
  48. $post['auth_token'] = $auth_token;
  49. $str = '?';
  50. foreach($post as $k=>$v){
  51. $str .= $k.'='.$v.'&';
  52. }
  53. $str = rtrim($str, '&');
  54. $url = self::$push_one_url.$str;
  55. //$data['target_value'] = '';
  56. $result = self::curl_get($url, $post);
  57. return $result;
  58. }
  59. public static function get_auth_token(){
  60. $data = array();
  61. $data['app_key'] = self::$appkey;
  62. $data['timestamp'] = time()*1000;
  63. $str = $data['app_key'].$data['timestamp'].self::$mastersecret;
  64. $data['sign'] = self::sha256($str);
  65. $url = self::$auth_token_url.'?app_key='.$data['app_key'].'&timestamp='.$data['timestamp'].'&sign='.$data['sign'];
  66. $result = self::curl_get($url, $data);
  67. if( $result['code'] == 0 && isset($result['data']['auth_token']) ){
  68. return $result['data']['auth_token'];
  69. }else{
  70. return false;
  71. }
  72. }
  73. # 保存消息体
  74. public static function save_message_content($desc,$auth_token){
  75. $data = self::push_data_format( $desc );
  76. $data['auth_token'] = $auth_token;
  77. $str = '?';
  78. foreach($data as $k=>$v){
  79. $str .= $k.'='.urlencode($v).'&';
  80. }
  81. $str = rtrim($str, '&');
  82. $url = self::$save_message_url.$str;
  83. $result = self::curl_get($url, $data);
  84. if( $result['code'] == 0 && isset($result['data']['message_id']) ){
  85. return $result['data']['message_id'];
  86. }else{
  87. return false;
  88. }
  89. }
  90. # 拼推送基本参数
  91. public static function push_data_format($desc){
  92. $data = array();
  93. $time = time();
  94. $data['app_message_id'] = $desc['app'].$time.rand(100,999);
  95. $data['title'] = $desc['title'];
  96. $data['sub_title'] = isset($desc['sub_title'])? $desc['sub_title'] : '';
  97. $data['content'] = $desc['message'];
  98. $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
  99. //$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
  100. //$data['click_action_url'] = isset($desc['click_action_url']) ? $desc['click_action_url'] : ''; //网页地址【click_action_type为2必填,长度500】
  101. //$data['action_parameters'] = isset($desc['action_parameters']) ? $desc['action_parameters'] : ''; //动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"}
  102. $data['off_line_ttl'] = isset($desc['off_line_ttl'])? $desc['off_line_ttl'] : 36000; //离线消息的存活时间(time_to_live) (单位:秒), 【off_line值为true时,必填,最长3天】
  103. $data['push_time_type'] = 0;//isset($desc['push_time_type'])? $desc['push_time_type'] : 1; //定时推送 (0, “即时”),(1, “定时”), 【只对全部用户推送生效】
  104. $data['push_start_time'] = isset($desc['push_start_time'])? $desc['push_start_time']+1000 : $time*1000; //定时推送开始时间(根据time_zone转换成当地时间), 【push_time_type 为1必填】,时间的毫秒数
  105. $data['show_time_type'] = 1; //展示类型 (0, “即时”),(1, “定时”)
  106. $data['show_start_time'] = isset($desc['show_start_time'])? $desc['show_start_time'] +2000: $time*1000;//定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数
  107. $data['show_end_time'] = isset($desc['show_start_time'])? $desc['show_start_time']+ 3000: ($time+3)*1000;//定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数
  108. $data['off_line'] = true;
  109. return $data;
  110. }
  111. /* PHP sha256() */
  112. public static function sha256($data, $rawOutput=false){
  113. if(!is_scalar($data)){
  114. return false;
  115. }
  116. $data = (string)$data;
  117. $rawOutput = !!$rawOutput;
  118. return hash('sha256', $data, $rawOutput);
  119. }
  120. public static function curl_get($url, $param){
  121. if(isset($param['auth_token']) ){
  122. $auth_token = $param['auth_token'];
  123. }else{
  124. $auth_token = '';
  125. }
  126. $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token);
  127. $ch = curl_init();//初始化curl
  128. curl_setopt($ch, CURLOPT_URL,$url);
  129. curl_setopt($ch, CURLOPT_HEADER, 0);//
  130. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  131. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  132. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  134. //curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  135. //curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  136. $data = curl_exec($ch);//运行curl
  137. curl_close($ch);
  138. return json_decode($data,true);
  139. }
  140. public static function curl_post($url, $param){
  141. $auth_token = isset($param['auth_token'])? $param['auth_token'] : '';
  142. unset($param['auth_token']);
  143. $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token);
  144. $ch = curl_init();//初始化curl
  145. curl_setopt($ch, CURLOPT_URL,$url);
  146. curl_setopt($ch, CURLOPT_HEADER, 0);//
  147. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  148. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
  150. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  151. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  152. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  153. $data = curl_exec($ch);//运行curl
  154. curl_close($ch);
  155. return json_decode($data,true);
  156. }
  157. }