how to send push notifications from Firebase using ASP.NET CORE 3.0 with FirebaseAdmin SDK?
Here I am a going to explains how to send mobile notifications from Firebase using ASP.NET CORE 3.0 with Firebase Admin SDK.
Follow the steps:
Step 1: Add the FirebaseAdmin package by NuGet.
Step 2: Get a Firebase service account key from Project Setting -> Service account -> Click on generate new private key.
Step 3: Initialize the FirebaseAdmin SDK from Startup.cs
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. Define Method for Sending 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 "";
}