Steps to get server key / SenderID
- To get the keys, log in to Firebase account.
- Select the project from the list.
- Click on "Setting" gear icon and click "Project Settings".

- Click on "Cloud Messaging" tab to select "Server key" and "Sender key" from the below screen.

- //Create the web request with fire base API
- 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}", serverKey));
- //Sender Id - From firebase project setting
- tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
- tRequest.ContentType = "application/json";
- var payload = new {
- to = deviceId,
- priority = "high",
- content_available = true,
- notification = new {
- body = txtmsg,
- title = txttitle.Replace(":", ""),
- sound = "sound.caf",
- badge = badgeCounter
- },
- };
- var serializer = new JavaScriptSerializer();
- Byte[] byteArray = Encoding.UTF8.GetBytes(payload);
- 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;
- }
- }
- }
- }
Ekta PatelPosted Mar 15, 2021, 1:13 PM
Hello, I am facing invalidRegestration error. and device id means mobile id? Please help me ..
MasPosted May 8, 2020, 6:37 AM
Members, here am getting The name result does not exist in the current context String sResponseFromServer = tReader.ReadToEnd(); result.Response = sResponseFromServer;
Indrawan IsmantoPosted Aug 23, 2019, 9:15 AM
I got error with payload variable. it said can't convert from < object > into char[]. would u help me please ?I got error with payload variable. it said can't convert from < object > into char[]. would u help me please ?
Aiswarya RamanPosted Jun 4, 2019, 12:55 AM
The code ran with no errors but I'm not receiving any notification. I used "to = "/topics/all"" but still no notification. can anyone please help.
Dhaval ThakkarPosted Dec 8, 2018, 12:26 PM
The remote server returned an error: (400) Bad Request. checked server key works fine with Postman.
mohamed fouadPosted Jul 15, 2018, 9:53 AM
I get this error Severity Code Description Project File Line Suppression StateError CS1503 Argument 1: cannot convert from '<anonymous type: string to, string priority, bool content_available, <anonymous type: string body, string title, string sound, string badge> notification>' to 'char[]'
Abhinav AgarwalPosted Apr 11, 2018, 8:30 AM
It Shows Success But No Notification Recieved
Alp ŞahinPosted Mar 27, 2018, 4:41 PM
And my third question where did we describe result variable
Alp ŞahinPosted Mar 27, 2018, 4:39 PM
Byte[] byteArray = Encoding.UTF8.GetBytes(payload); This line says payload is anonymous type and cannot convertable
Alp ŞahinPosted Mar 27, 2018, 4:23 PM
How can i send all users what should i write
nurhidayat droidPosted Feb 23, 2018, 3:08 AM
Do you have sample for net core 2.0 ?
Nagaraj SPosted Feb 6, 2018, 5:39 AM
How do i set notification icon ?? i have used this but default notification icon only showing var data = new { to = deviceId, notification = new { body = "Welcome", title = "hey", icon = "https://placeimg.com/250/250/nature" } };
Gajendra JangidPosted Feb 3, 2018, 6:24 AM
Vary usefull article....................
Srinivas chPosted Feb 1, 2018, 8:32 AM
I am using same block of code getting " The remote server returned an error: (400) Bad Request." issue with "An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code" Exception
MILAN TIPLEPosted Dec 20, 2017, 7:00 AM
Good explaination.