.net 华为离线消息推送

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&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值