Nav apraksta

HuaweiPush.php 4.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. class HuaweiPush{
  3. private static $appid;
  4. private static $appsecret;
  5. private static $auth_token_url = 'https://login.cloud.huawei.com/oauth2/v2/token';
  6. private static $push_url = 'https://api.push.hicloud.com/pushsend.do?nsp_ctx=';
  7. private static $package;
  8. # 安卓初始参数配置
  9. public static function _init($desc){
  10. self::$appsecret = $desc['appsecret'];
  11. self::$appid = $desc['appid'];
  12. self::$package = $desc['package'];
  13. $str = urlencode("{\"ver\":\"1\", \"appId\":\"" . self::$appid . "\"}");
  14. self::$push_url .= $str;
  15. }
  16. # 安卓推所有(push_time_type:1 定时,0即时)
  17. public static function pushAndroid($desc){
  18. //参数:app_key timestamp sign:sha256(appkey+timestamp+mastersecret)
  19. self::_init($desc);
  20. $access_token = self::get_auth_token();
  21. if( !$access_token ) return false;
  22. $time = time();
  23. $data = array();
  24. $data['access_token'] = $access_token;
  25. $data['nsp_ts'] = $time;
  26. $data['nsp_svc'] = 'openpush.message.api.send';
  27. $data['device_token_list'] = $desc['device_token']; //JSON数值字符串,单次最多只是1000个。例如(未编码前):["12345xxxxxxxxxxxxx23456","2234567xxxxxxxxxx123456","086200503xxxxxxxxxxxxxxx300CN01"]
  28. $data['expire_time'] = date("Y-m-dTH:i", strtotime('+6 hour'));//如果用户没有在线,此消息会保存到PUSH服务器的时间。
  29. $payload = array();
  30. $payload['hps']['msg']['type'] = 3;//1 透传异步消息3 系统通知栏异步消息注意:2和4以后为保留后续扩展使用
  31. $payload['hps']['msg']['body']['content'] = $desc['message'];
  32. $payload['hps']['msg']['body']['title'] = $desc['title'];
  33. $payload['hps']['msg']['action']['type'] = 3;// 1 自定义行为:行为由参数intent定义2 打开URL:URL地址由参数url定义3 打开APP:默认值,打开App的首页
  34. //$payload['hps']['msg']['action']['param']['intent'] = '';//Action的type为1的时候表示自定义行为。
  35. //$payload['hps']['msg']['action']['param']['url'] = '';//Action的type为2的时候表示打开URL地址
  36. $payload['hps']['msg']['action']['param']['appPkgName'] = self::$package;
  37. $payload['hps']['ext']['biTag'] = isset($desc['type'])? $desc['type'] : 'Trump';
  38. //$payload['hps']['ext']['customize'] = isset($desc['ext'])? $desc['ext'] : '';
  39. //描述投递消息的JSON结构体,描述PUSH消息的:类型、内容、显示、点击动作、报表统计和扩展信息
  40. $postBody = 'access_token=' . urlencode($data['access_token']) . '&nsp_svc=' . urlencode('openpush.message.api.send') . '&nsp_ts=' . (int)urlencode($data['nsp_ts']) . '&device_token_list=' . urlencode(json_encode($data['device_token_list'])) . '&payload=' . urlencode(json_encode($payload));
  41. $result = self::curl_post(self::$push_url, $postBody);
  42. return json_decode($result,true);
  43. }
  44. public static function get_auth_token(){
  45. //grant_type=client_credentials&client_secret=申请应用时获得的应用密钥&client_id=申请应用时获得的应用ID
  46. $data = array();
  47. $data['grant_type'] = 'client_credentials';
  48. $client_id = self::$appid;
  49. $client_secret = self::$appsecret;
  50. $str = 'grant_type=client_credentials&client_secret=' . $client_secret . '&client_id=' . $client_id;
  51. //$data['scope'] = 'nsp.auth nsp.user nsp.vfs nsp.ping openpush.message'; //权限列表,默认nsp.auth nsp.user nsp.vfs nsp.ping openpush.message
  52. $result = self::curl_post(self::$auth_token_url, $str);
  53. $result = json_decode($result,true);
  54. if( isset($result['access_token']) ){
  55. return $result['access_token'];
  56. }else{
  57. return false;
  58. }
  59. }
  60. public static function curl_post($url, $param){
  61. $postUrl = $url;
  62. $curlPost = $param;
  63. $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8');
  64. $ch = curl_init();//初始化curl
  65. curl_setopt($ch, CURLOPT_URL,$postUrl);
  66. curl_setopt($ch, CURLOPT_HEADER, 0);//
  67. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  69. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  70. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  71. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  72. $data = curl_exec($ch);//运行curl
  73. curl_close($ch);
  74. return $data;
  75. }
  76. }