1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- class HuaweiPush{
- private static $appid;
- private static $appsecret;
- private static $auth_token_url = 'https://login.cloud.huawei.com/oauth2/v2/token';
- private static $push_url = 'https://api.push.hicloud.com/pushsend.do?nsp_ctx=';
- private static $package;
- # 安卓初始参数配置
- public static function _init($desc){
- self::$appsecret = $desc['appsecret'];
- self::$appid = $desc['appid'];
- self::$package = $desc['package'];
- $str = urlencode("{\"ver\":\"1\", \"appId\":\"" . self::$appid . "\"}");
- self::$push_url .= $str;
- }
-
- # 安卓推所有(push_time_type:1 定时,0即时)
- public static function pushAndroid($desc){
- //参数:app_key timestamp sign:sha256(appkey+timestamp+mastersecret)
- self::_init($desc);
- $access_token = self::get_auth_token();
- if( !$access_token ) return false;
- $time = time();
- $data = array();
- $data['access_token'] = $access_token;
- $data['nsp_ts'] = $time;
- $data['nsp_svc'] = 'openpush.message.api.send';
- $data['device_token_list'] = $desc['device_token']; //JSON数值字符串,单次最多只是1000个。例如(未编码前):["12345xxxxxxxxxxxxx23456","2234567xxxxxxxxxx123456","086200503xxxxxxxxxxxxxxx300CN01"]
- $data['expire_time'] = date("Y-m-dTH:i", strtotime('+6 hour'));//如果用户没有在线,此消息会保存到PUSH服务器的时间。
- $payload = array();
- $payload['hps']['msg']['type'] = 3;//1 透传异步消息3 系统通知栏异步消息注意:2和4以后为保留后续扩展使用
- $payload['hps']['msg']['body']['content'] = $desc['message'];
- $payload['hps']['msg']['body']['title'] = $desc['title'];
- $payload['hps']['msg']['action']['type'] = 3;// 1 自定义行为:行为由参数intent定义2 打开URL:URL地址由参数url定义3 打开APP:默认值,打开App的首页
- //$payload['hps']['msg']['action']['param']['intent'] = '';//Action的type为1的时候表示自定义行为。
- //$payload['hps']['msg']['action']['param']['url'] = '';//Action的type为2的时候表示打开URL地址
- $payload['hps']['msg']['action']['param']['appPkgName'] = self::$package;
- $payload['hps']['ext']['biTag'] = isset($desc['type'])? $desc['type'] : 'Trump';
- //$payload['hps']['ext']['customize'] = isset($desc['ext'])? $desc['ext'] : '';
- //描述投递消息的JSON结构体,描述PUSH消息的:类型、内容、显示、点击动作、报表统计和扩展信息
- $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));
-
- $result = self::curl_post(self::$push_url, $postBody);
- return json_decode($result,true);
- }
- public static function get_auth_token(){
- //grant_type=client_credentials&client_secret=申请应用时获得的应用密钥&client_id=申请应用时获得的应用ID
- $data = array();
- $data['grant_type'] = 'client_credentials';
- $client_id = self::$appid;
- $client_secret = self::$appsecret;
- $str = 'grant_type=client_credentials&client_secret=' . $client_secret . '&client_id=' . $client_id;
- //$data['scope'] = 'nsp.auth nsp.user nsp.vfs nsp.ping openpush.message'; //权限列表,默认nsp.auth nsp.user nsp.vfs nsp.ping openpush.message
- $result = self::curl_post(self::$auth_token_url, $str);
- $result = json_decode($result,true);
- if( isset($result['access_token']) ){
- return $result['access_token'];
- }else{
- return false;
- }
- }
- public static function curl_post($url, $param){
- $postUrl = $url;
- $curlPost = $param;
- $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8');
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL,$postUrl);
- curl_setopt($ch, CURLOPT_HEADER, 0);//
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return $data;
- }
- }
|