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();
}
}