C# 下的华为手机通知栏消息推送,采用的是先获取token,再调用接口的形式,代码如下:
需要注意的是: 终端ID 需要开发Android端的人调用HMS.SDK提供
/// <summary>
/// 华为推送消息方法
/// </summary>
public class HuaWeiOutlineMsgSend
{
/// <summary>
/// 发送通知消息
/// </summary>
/// <param name="infoContent">序列化后的消息体</param>
/// <param name="appSecret">用户在华为开发者联盟申请Push服务获取的服务参数</param>
/// <param name="appId">用户在华为开发者联盟申请Push服务获取的服务参数</param>
/// <returns></returns>
public static string RefreshToken(string infoContent, string appSecret, string appId)
{
if (string.IsNullOrWhiteSpace(infoContent)) return "华为推送消息内容为空,无法推送";
if (string.IsNullOrWhiteSpace(appSecret)) return "华为推送appSecret为空,无法推送";
if (string.IsNullOrWhiteSpace(appId)) return "华为推送appId为空,无法推送";
string tokenUrl = "https://login.cloud.huawei.com/oauth2/v2/token"; //获取认证Token的URL
string apiUrl = "https://push-api.cloud.huawei.com/v1/" + appId + "/messages:send"; //应用级消息下发API
string accessToken = string.Empty; //下发通知消息的认证Token
Encoding encode = Encoding.UTF8;
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("grant_type", "client_credentials");
dic.Add("client_secret", System.Web.HttpUtility.UrlEncode(appSecret, Encoding.UTF8));
dic.Add("client_id", appId);
var content = new FormUrlEncodedContent(dic);
var http = new HttpClient();
var response = http.PostAsync(tokenUrl, content);
string text = response.Result.Content.ReadAsStringAsync().Result;
HWclass hw = JsonConvert.DeserializeObject<HWclass&