Introduction
This blog post explains how to send FCM Push notifications as a Firebase admin. There are many Google Firebase tutorials available on the subject, but they often not clear and step-by-step. As such, here is my code, I hope this helps everyone.
1. Add the Firebase admin .net SDK by NuGet

2. Get a Firebase service account key from Project Setting -> Service account -> Click on generate new private key.
3. Initialize the Firebase Admin SDK in startup.cs
- /* - - - - - - - - - - FCM Admin SDK - - - - - - - - - - - - - - - - */
- public IServiceProvider ConfigureServices(IServiceCollection services)
- var googleCredential = _hostingEnvironment.ContentRootPath;
- var filePath = Configuration.GetSection("GoogleFirebase")["fileName"];
- googleCredential = Path.Combine(googleCredential, filePath);
- var credential = GoogleCredential.FromFile(googleCredential);
- FirebaseApp.Create(new AppOptions()
- {
- Credential = credential
- });
- }
4. Send Notification
- public virtual async Task<string> SendNotification(List<string> clientToken, string title, string body)
- {
- var registrationTokens = clientToken;
- var message = new MulticastMessage()
- {
- Tokens = registrationTokens,
- Data = new Dictionary<string, string>()
- {
- {"title", title},
- {"body", body},
- },
- };
- var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message).ConfigureAwait(true);
- return "";
- }


ria maryPosted Feb 18, 2022, 10:56 AM EST
1>Startup.cs(29,35,29,54): error CS0103: The name '_hostingEnvironment' does not exist in the current context, How to resolve this,kindly reply
Chirag RathodPosted May 11, 2021, 1:03 PM EDT
This code working fine for android device but not working for ios.can you please suggest how to using it for IOS
nawaz shareefPosted Feb 17, 2021, 1:50 PM EST
Add Notification object in MulticastMessage
nawaz shareefPosted Feb 17, 2021, 1:40 PM EST
Var message = new MulticastMessage() { Tokens = registrationTokens, Notification = new Notification { Body = body, Title = title } }; var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message).ConfigureAwait(true);
ahmed salimPosted Dec 27, 2020, 3:32 PM EST
However i got success response but my device not received any message , do you have any idea
Manish KumarPosted Sep 1, 2020, 1:29 AM EDT
How we will get the list of clientToken to send notifications?
Jaifran HernándezPosted Aug 27, 2020, 7:12 AM EDT
Where do i place the send notification code? On a new file or what? I dont get it
Dhanunjai RaoPosted Aug 1, 2020, 8:59 PM EDT
I'm getting FirebaseMessaging.DefaultInstance is null in my C# code, when the default instance is null please help me in this issue...
Vipin GroverPosted Jul 9, 2020, 11:59 PM EDT
What is client token and how will I get this?
nasser fetPosted May 29, 2020, 11:38 AM EDT
hi, my code is stucked at: await hiFirebaseMessaging.DefaultInstance.SendMulticastAsync(message).ConfigureAwait(true)
Rafael BolanosPosted May 19, 2020, 5:28 PM EDT
Thank Dharmesh.
Kamil KostrzewskiPosted Apr 28, 2020, 11:19 PM EDT
``` return "";``` why? You can just use Task type, and its equivalent of "void" but async.
SamPosted Dec 7, 2019, 1:02 AM EST
Var filePath = Configuration.GetSection("GoogleFirebase")["fileName"]; can you explain it more or send me complete code
SamPosted Dec 7, 2019, 1:02 AM EST
_hostingEnvironment ??