华为推送

转载于:https://blog.csdn.net/qq_23564667/article/details/108662377

/**
 * 发送Push消息
 * @param array $deviceTokens 指定发送用户
 * @param string $title 推送标题
 * @param string $description 通知栏消息内容
 * @param string $page 快应用界面的path
 * @param array $params 快应用界面时携带的参数
 * @return bool|string
 * @throws Exception
 * 官方文档: https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-access-push-kit#h2-1577437411178
 */
function sendPushMessage($deviceTokens=[],$title='',$description='',$page='',$params=[]){
    $appId = "102156515";//用户在华为开发者联盟申请的appId和appSecret(会员中心->我的应用, 点击“应用信息”获取)
    $appSecret = "1a17b05e0cfc72395a9e3aac1385dbf3af99103b86fac5adf860c76bdba14901";
    $accessToken =  $this->refreshToken($appId,$appSecret);

    /*PushManager.requestToken为客户端申请token的方法,可以调用多次以防止申请token失败*/
    /*PushToken不支持手动编写,需使用客户端的onToken方法获取*/
//        $deviceTokens = array();//目标设备Token
//        $deviceTokens[] = 'AAE4PxueamHwa2NurzvsV7jBJut4arMrCRKkPJ1_QWxikUpQxXXPWFVF-Gue5gL4bdJlKe5YFySZOyMPqqGgLKma8PLBljy76_q9Q6KIKhLPy9AacqFphwpHy80IX091Yg';
//        $title='觉醒前世记忆,重走弑天道,这一世,注定不平凡...';
//        $description='觉醒前世记忆,重走弑天道,这一世,注定不平凡...';
//        $page='/';
    $params["key1"] = "test1";

    // page   点击通知栏消息需要跳转到的快应用界面的path,Pushtype为0时有效。
    // pushtype    0:通知栏消息 ● 1:透传消息
    // validate_only   可选 true:测试消息   false:正式消息
    // fast_app_target   1:加载器方式 2:引擎方式
    $params = addslashes(json_encode($params));
    $postBody = '{
         "validate_only": false,
         "message": {
          "data": "{\"pushtype\":0,\"pushbody\":{\"title\":\"'.$title.'\",\"description\":\"'.$description.'\",\"page\":\"'.$page.'\",\"params\":'.$params.',\"ringtone\":{\"vibration\":\"true\",\"breathLight\":\"true\"}}}",
          "ssss":"{k1:v1}",
          "android": {
           "collapse_key": -1,
           "delivery_priority": "HIGH",
           "ttl": "1448s",
           "bi_tag": "Trump",
           "fast_app_target": 2
          },
          "token": '.json_encode($deviceTokens).'
         }
        }';
    $accessToken = str_replace("\\",'',$accessToken);
    $header[] = "Content-type: application/json";
    $header[] = "Authorization: Bearer {$accessToken}";
    $postUrl = "https://push-api.cloud.huawei.com/v1/{$appId}/messages:send";
    $response = $this->curl_send($postUrl, $postBody,$header);
    $response = json_decode($response,true);
    $response['postBody'] = $postBody;
    return $response;
}

/**
 * 请求AccessToken
 * @param string $client_id  申请应用时获得的应用 ID,对应华为开发者联盟网站申请应用的时候获取的AppID。
 * @param string $client_secret 申请应用时获得的应用密钥,对应华为开发者联盟网站申请应用的时候获取的AppSecret。
 * @return bool
 * @throws Exception
 * 官方文档:https://developer.huawei.com/consumer/cn/doc/development/HMS-2-References/hmssdk_huaweipush_api_reference_agent_s1
 */
private function refreshToken($client_id='',$client_secret=''){
    if(empty($client_id) || empty($client_secret)){
        return false;
    }
    $key = 'kyy_huawei_token_'.$client_id;
    $response = S($key);
    if(empty($response)){
        $postData = [
            'grant_type'=>'client_credentials',
            'client_secret'=>$client_secret,
            'client_id'=>$client_id,
        ];
        $tokenUrl = 'https://login.cloud.huawei.com/oauth2/v2/token'; //获取认证Token的URL
        $response = $this->curl_send($tokenUrl, http_build_query($postData));
        $response = json_decode($response,true);
        S($key,$response,3500);
    }
    return $response['access_token'];
}

/**
 * 请求curl
 * @param $url
 * @param $void
 * @param $void
 * @return bool|string
 * @throws Exception
 */
public static function curl_send($url,$postData,$header){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5000);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5000);

    if($header){
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    }

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlErrNo = curl_errno($ch);
    $curlErr = curl_error($ch);
    curl_close($ch);
    return $result;
}

  

上一篇:Shell面试题


下一篇:DBUtils ResultSetHandeler常用的处理类