ikrami sami

ikrami sami

  • NA
  • 193
  • 22.8k

push notification to android using c#

Dec 28 2019 4:43 PM
The below code i found in some websites but it always return error
multicast_id":6719893729607364541,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Code is:
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
//serverKey - Key from Firebase cloud messaging server
tRequest.Headers.Add(string.Format("Authorization: key={0}", "Server key"));
//Sender Id - From firebase project setting
tRequest.Headers.Add(string.Format("Sender: id={0}", "Sender Id"));
tRequest.ContentType = "application/json";
var payload = new
{
to = "//topics//news", // even "/topics/news:
priority = "high",
content_available = true,
notification = new
{
body = "Test",
title = "Test",
badge = 1
},
data = new
{
key1 = "value1",
key2 = "value2"
}
};
string postbody = JsonConvert.SerializeObject(payload).ToString();
Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
//result.Response = sResponseFromServer;
this.Label.Text = sResponseFromServer;
}
}
}
}

Answers (2)