使用google cloud message,asp.net core web api发送通知

时间:2017-06-09 06:16:35

标签: asp.net-web-api asp.net-core google-cloud-messaging asp.net-core-mvc

我尝试使用Google Cloud Message向我的Android客户端发送通知,但我不知道该怎么做。这是我的后端代码asp.net核心1.1,但它正在工作:((

var client = new HttpClient();
            client.BaseAddress = new Uri("https://android.googleapis.com/gcm/send");
            string url = "https://android.googleapis.com/gcm/send";
            client.DefaultRequestHeaders.Add("Authorization", SERVER_API_KEY);
            client.DefaultRequestHeaders.Add("Sender", SENDER_ID);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");
            request.Content = new StringContent("application/x-www-form-urlencoded;charset=UTF-8",
                                                Encoding.UTF8,
                                                    "application/json");//CONTENT-TYPE header

            string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + "test" + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";
            Console.WriteLine(postData);
            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            request.Content.Headers.ContentLength = byteArray.Length;

            await client.SendAsync(request)
              .ContinueWith(responseTask => 
              {
                 Console.WriteLine("Response: {0}", responseTask.Result);
              });

0 个答案:

没有答案