?? Sending Firebase Push Notifications to mobile apps in ASP.NET Core
Sandeep Pal
9+ Exp | C# | Dotnet Core | Web API | MVC 5 | Azure | React Js | Node JS | Microservices | Sql | MySQL | JavaScript | UnitTest | Dapper | Linq | DSA | Entity framework Core | Web Forms | Jquery | MongoDB | Quick Learner
In the world of mobile apps, push notifications are essential for keeping users engaged. Today, I’ll walk you through how to send push notifications to iOS and Android devices using Firebase Cloud Messaging (FCM) with an ASP.NET Core 8.0 API.
?? What you need:
?? Step-by-step Guide:
- Create Firebase Project: Get your Firebase Server Key and Sender ID from the Firebase Console.
- Install NuGet Packages: Install FirebaseAdmin and Google.Apis.Auth packages to communicate with Firebase
Install-Package FirebaseAdmin
Install-Package Google.Apis.Auth
- Configure Firebase in Program.cs:
- Create a Push Notification Service:
- Send Notifications via API:
领英推è
?? Sample Code:
// Firebase Initialization in Program.cs
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile("path/to/your/serviceAccountKey.json"),
});
// Push Notification Service
public class PushNotificationService
{
private readonly FirebaseMessaging _firebaseMessaging;
public PushNotificationService()
{
_firebaseMessaging = FirebaseMessaging.DefaultInstance;
}
public async Task SendPushNotificationAsync(string deviceToken, string title, string body)
{
var message = new Message()
{
Token = deviceToken,
Notification = new Notification { Title = title, Body = body }
};
string response = await _firebaseMessaging.SendAsync(message);
Console.WriteLine("Successfully sent message: " + response);
}
}
// Define the controller to send notifications
[ApiController]
[Route("api/[controller]")]
public class NotificationsController : ControllerBase
{
private readonly PushNotificationService _pushNotificationService;
public NotificationsController(PushNotificationService pushNotificationService)
{
_pushNotificationService = pushNotificationService;
}
[HttpPost("send-notification")]
public async Task<IActionResult> SendNotification([FromBody] NotificationRequest request)
{
// Send push notification to the device
await _pushNotificationService.SendPushNotificationAsync(request.DeviceToken, request.Title, request.Body);
return Ok(new { message = "Notification sent successfully" });
}
}
public class NotificationRequest
{
public string DeviceToken { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
?? Key Benefits:
- Cross-platform support: Works for both iOS and Android.
- Real-time notifications: Push notifications can be sent instantly, helping engage users.
- Easy integration: With Firebase Admin SDK, it's easy to integrate Firebase services into your ASP.NET Core app.
?? Ready to try this out?
If you’re working with mobile apps and want to add push notifications, integrating Firebase Cloud Messaging (FCM) with ASP.NET Core is a powerful and simple solution.
#dotnet #firebase #aspnetcore #pushnotifications #developer #mobiledevelopment #webdevelopment #technology