How to send Firebase notifications in C#

This post explains how to send mobile notifications from Firebase in C# with Firebase Admin SDK

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

  1. /* - - - - - - - - - - FCM Admin SDK - - - - - - - - - - - - - - - - */  
  2. public IServiceProvider ConfigureServices(IServiceCollection services)  
  3. var googleCredential = _hostingEnvironment.ContentRootPath;  
  4.  var filePath = Configuration.GetSection("GoogleFirebase")["fileName"];  
  5.  googleCredential = Path.Combine(googleCredential, filePath);  
  6.  var credential = GoogleCredential.FromFile(googleCredential);  
  7.  FirebaseApp.Create(new AppOptions()  
  8.  {  
  9.    
  10.  Credential = credential  
  11.  });  
  12. }  

4. Send Notification

  1. public virtual async Task<string> SendNotification(List<string> clientToken, string title, string body)  
  2.  {  
  3.  var registrationTokens = clientToken;  
  4.  var message = new MulticastMessage()  
  5.  {  
  6.  Tokens = registrationTokens,  
  7.  Data = new Dictionary<stringstring>()  
  8.  {  
  9.  {"title", title},  
  10.  {"body", body},  
  11.  },  
  12.  };  
  13.  var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(message).ConfigureAwait(true);  
  14.  return "";  
  15.  }  
That's it.