PHP
PHP调用华为快应用消息推送接口
PHP通过curl方式调用华为快应用消息推送接口
继上一篇推送vivo快应用接口,这一篇跟大家分享调用华为快应用消息推送接口。
首先是调用请求逻辑,代码如下:
/** * 华为请求发送 * @param $remote_server * @param $post_string * @param string $authToken * @return mixed */ function huaWeiRequestByCurl($remote_server, $post_string, $authToken = "") { $authToken = str_replace("\\", "", $authToken); $Bearer = "Bearer " . $authToken; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_server); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //根据情况展示 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json;charset=utf-8', 'Authorization:' . $Bearer, )); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); curl_close($ch); return $data; } /** * 华为curl请求 * @param $dataParams * @param $urlApi * @return mixed */ function huaWeiCurlPost($dataParams, $urlApi) { $data = http_build_query($dataParams); //初使化init方法 $ch = curl_init(); //指定URL curl_setopt($ch, CURLOPT_URL, $urlApi); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'); //设定请求后返回结果 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //声明使用POST方式来进行发送 curl_setopt($ch, CURLOPT_POST, 1); //发送什么数据呢 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //忽略证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //忽略header头信息 curl_setopt($ch, CURLOPT_HEADER, 0); //设置超时时间 curl_setopt($ch, CURLOPT_TIMEOUT, 10); //发送请求 $output = curl_exec($ch); //关闭curl curl_close($ch); #json转数组 $result = json_decode($output, true); #返回 return $result; }
然后是获取AccessToken,代码如下:
/** * 华为-获取AccessToken * @param string $client_id * @param string $client_secret * @return string */ function getHuaWeiAccessToken($client_id = "", $client_secret = "") { #请求地址 $url = "https://login.cloud.huawei.com/oauth2/v2/token"; #组合请求数据 $dataParams = array( "grant_type" => "client_credentials", "client_id" => $client_id, "client_secret" => $client_secret, ); #调取接口 $getAccessToken = huaWeiCurlPost($dataParams, $url); #判断是否调用成功 if (!isset($getAccessToken["access_token"])||(empty($getAccessToken["access_token"]))){ return ""; } else { return $getAccessToken["access_token"]; } }
然后是消息推送逻辑,代码如下:
$client_id = "自己的client_id"; $client_secret = "自己的client_secret"; $accessToken = getHuaWeiAccessToken($client_id, $client_secret); $userToken = "用户的regId"; $url = "https://push-api.cloud.huawei.com/v1/{$client_id}/messages:send"; $messageParams = array( "pushtype" => 0, "pushbody" => array( "title" => "消息通知!", "description" => "系统通知", "page" => "/", "params" => array("key1"=>"123"), "ringtone" => array( "vibration" => "true", "breathLight" => "true" ) ), ); $dataParams = array( "validate_only" => false, "message" => array( "data" => json_encode($messageParams), "android" => array("fast_app_target" => 2), "token" => array($userToken), ), ); $post_string = json_encode($dataParams); $data = huaWeiRequestByCurl($url, $post_string, $accessToken); $result = json_decode($data, true);
这样就完成了华为消息推送逻辑,但是不同vivo快应用消息推送,华为快应用消息推送一天只能接收到一次,也就是说,你发过一次,后面无论如何都是显示成功,但是设备不会再次收到。
0条评论