Send firebase cloud message C#

0 thích 0 không thích
14 lượt xem
đã hỏi 3 Tháng 10, 2018 bởi nguyenthao (9,040 điểm)
public string SendMessage(string title, string body, string tag, string to, string picture)
        {
            string serverKey = "MY_SERVER_KEY";

            try
            {
                var result = "-1";
                var webAddr = "https://fcm.googleapis.com/fcm/send";

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Headers.Add("Authorization", "Key=" + serverKey);
                httpWebRequest.Method = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {

                    string data = $@"
                       {{
                          ""data"": {{
                            ""body"": ""{body}"",
                            ""title"": ""{title}"",
                            ""sound"": ""default"",
                            ""tag"": ""{tag}"",
                            ""to"": ""{to}"",
                            ""picture"": ""{picture}""
                          }},
                          ""to"": ""/topics/MyTopic"",
                          ""priority"" : ""high"",
                          ""delay_while_idle"" : true,
                          ""content_available"" : true
                        }}
                    ";

                    //data = "{\"}";

                    richTextBox1.Text = data;
                    streamWriter.Write(data);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

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

                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return ex.ToString();
            }
        }
Looking for an answer?  Share this question:     

Xin vui lòng đăng nhập hoặc đăng ký để trả lời câu hỏi này.

...