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, 3:56 PM
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, 5:03 PM
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, 6:50 PM
Add Notification object in MulticastMessage
nawaz shareefPosted Feb 17, 2021, 6:40 PM
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, 8:32 PM
However i got success response but my device not received any message , do you have any idea
Manish KumarPosted Sep 1, 2020, 5:29 AM
How we will get the list of clientToken to send notifications?
Jaifran HernándezPosted Aug 27, 2020, 11:12 AM
Where do i place the send notification code? On a new file or what? I dont get it
Dhanunjai RaoPosted Aug 2, 2020, 12:59 AM
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 10, 2020, 3:59 AM
What is client token and how will I get this?
nasser fetPosted May 29, 2020, 3:38 PM
hi, my code is stucked at: await hiFirebaseMessaging.DefaultInstance.SendMulticastAsync(message).ConfigureAwait(true)
Rafael BolanosPosted May 19, 2020, 9:28 PM
Thank Dharmesh.
Kamil KostrzewskiPosted Apr 29, 2020, 3:19 AM
``` return "";``` why? You can just use Task type, and its equivalent of "void" but async.
SamPosted Dec 7, 2019, 6:02 AM
Var filePath = Configuration.GetSection("GoogleFirebase")["fileName"]; can you explain it more or send me complete code
SamPosted Dec 7, 2019, 6:02 AM
_hostingEnvironment ??