使用FCM通过c#WebApi向android发送通知

时间:2017-09-05 10:24:29

标签: c# asp.net-web-api notifications

        var webAddr = "https://fcm.googleapis.com/fcm/send";

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Headers.Add("Authorization:key=" + "My application ID");
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"to\": \"/topics/news\",\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\",}}";


            streamWriter.Write(json);
            streamWriter.Flush();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }

这是我正在尝试的但是得到

的错误

System.dll

中出现未处理的“System.Net.WebException”类型异常

其他信息:远程服务器返回错误:(401)未经授权。

1 个答案:

答案 0 :(得分:0)

按如下方式更改有效负载,它将正常工作 {“to”:这是你的令牌“,”通知“:{”body“:”新消息新消息ew essage new message new mw tech“,”title“:”Tesing596“,”icon“:”myicon“, “声音”:mySound“,”click_action“:”android.PS“}}