How to send Firebase notifications in C#

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  
     });  
    }  

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 "";  
     }  

Thanks

Dharmesh Sharma