<?php
header("Content-Type:text/html;charset=utf-8");
class huaweipush
{
private $app_id;
private $app_secret;
private $app_pkg;
public function __construct($appid,$appsecret,$apppkg)
{
$this->app_id = $appid;
$this->app_secret = $appsecret;
$this->app_pkg = $apppkg;
}
public function sendMsg()
{
$url1='https://login.vmall.com/oauth2/token';
//获取token
$huaweiToken=$this->get_access_token($url1,$this->app_id,$this->app_secret);
$body = array();//仅通知栏消息需要设置标题和内容,透传消息key和value为用户自定义
$body['title'] = 'Push message title cz';//消息标题
$body['content'] = 'Push message content cz';//消息标题
$param = array();
$param['appPkgName'] =$this->app_pkg; //定义需要打开的appPkgName
$action = array();
$action['param'] = $param;//消息点击动作参数
$action['type'] = 3;//类型3为打开APP,其他行为请参考接口文档设置
$msg = array();
$msg['action'] = $action;//消息点击动作
$msg['type'] = 3;//3: 通知栏消息,异步透传消息请根据接口文档设置
$msg['body'] = $body;//通知栏消息body内容
$ext = array();//扩展信息,含BI消息统计,特定展示风格,消息折叠。
$ext['biTag'] = 'Trump';//设置消息标签,如果带了这个标签,会在回执中推送给CP用于检测某种类型消息的到达率和状态
$ext['icon'] = "";//自定义推送消息在通知栏的图标,value为一个公网可以访问的URL
$hps = array();//华为PUSH消息总结构体
$hps['msg'] = $msg;
$hps['ext'] = $ext;
//var_dump($hps);
//注意device_token_list为设备端获取的token,可以多个
$payload = array();
$payload['hps'] = $hps;
$res = $this->request(
'https://api.push.hicloud.com/pushsend.do?nsp_ctx=' . urlencode('{"ver":"1", "appId":"你的APPID"}'),
[
'access_token' => $huaweiToken,
'nsp_svc' => 'openpush.message.api.send',
'nsp_ts' => (int)time(),
'device_token_list' => json_encode(['0866592036543584300002739400CN01','22345678901234561234567890123456']),,
'payload' => json_encode($payload),
],
['Content-Type: application/x-www-form-urlencoded; charset=utf-8'],
true
);
var_dump($res);//查看结果
}
public function get_access_token($url,$app_id,$app_secret)
{
$res = file_get_contents('./AccessToken/access_token.json');
$result = json_decode($res, true);
$this->expires_time = $result["expires_time"];
$accessToken = $result["access_token"];
if (time() > ($this->expires_time + 3600))
{
$res = $this->request(
$url,
[
'grant_type' => 'client_credentials',
'client_secret' => $app_secret,
'client_id' => $app_id
],
['Content-Type: application/x-www-form-urlencoded; charset=utf-8'],
true
);
$resArray = json_decode($res, true);
//var_dump($resArray);
$accessToken = $resArray['access_token'];
echo "Token".$accessToken."\n";
$this->expires_time = time();
file_put_contents('./AccessToken/access_token.json', '{"access_token": "'.$accessToken.'", "expires_time": '.$this->expires_time.'}');
}
echo "accessToken-> ".$accessToken;
return $accessToken;
}
function request($url, $postData = [], $header = [], $formUrlencoded = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
if ($postData) {
curl_setopt($ch, CURLOPT_POST, true);
//如果不用http_build_query你就会踩到坑的,你可以试试
if($formUrlencoded){
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
}else{
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
}
$response = curl_exec($ch);
if ($errno = curl_errno($ch)) {
$error = curl_error($ch);
$this->errmsg = $error;
$this->errno = $errno;
curl_close($ch);
return false;
}
curl_close($ch);
return $response ;
}
}
?>