Fcm firebase cloud messaging push notification with asp net


ASP.NET is a popular programming language used for web applications. In this article, we will explore how to implement FCM (Firebase Cloud Messaging) push notifications in an ASP.NET application. Push notifications are a great way to engage users and keep them informed about important updates or events.

Setting up Firebase Cloud Messaging

Before we dive into the code, we need to set up Firebase Cloud Messaging in our ASP.NET application. Follow these :

  1. Create a Firebase project in the Firebase console.
  2. Enable Firebase Cloud Messaging in the project settings.
  3. Generate a server key or legacy server key in the Cloud Messaging tab.
  4. Copy the server key for later use in our ASP.NET code.

Implementing FCM Push Notifications in ASP.NET

Now that we have set up Firebase Cloud Messaging, let's see how we can implement push notifications in our ASP.NET application.

Step 1: Install the FirebaseAdmin NuGet Package

First, we need to install the FirebaseAdmin NuGet package, which provides the necessary APIs to with Firebase Cloud Messaging. Open the NuGet Package Manager Console and run the following command:

Install-Package FirebaseAdmin

Step 2: Initialize FirebaseApp

Next, we need to initialize FirebaseApp with our Firebase project credentials. This should be done at the application startup. Add the following code to your Startup.cs file:

using Google.Apis.Auth.OAuth2;

// ...

public void ConfigureServices(IServiceCollection services)
    // ...

    FirebaseApp.Create(new AppOptions()
         = GoogleCredential.FromFile("path/to/serviceAccountKey.json"),

Make sure to replace “path/to/serviceAccountKey.json” with the actual path to your key JSON file.

Step 3: Sending Push Notifications

Now, let's see how we can send push notifications to devices using FCM. Add the following code to your controller or service:

using FirebaseAdmin.Messaging;

// ...

public void SendPushNotification(string deviceToken, string , string body)
    var message = new Message()
        Token = deviceToken,
        Notification = new Notification()
            Title = title,
            Body = body,


In the above code, we create a new Message object with the device token, title, and body of the notification. We then use FirebaseMessaging.DefaultInstance.SendAsync to send the message to the device.

Step 4: Handling Push Notifications on the

On the client side, you need to handle the received push notifications. This can be done using JavaScript or any other client-side technology. Here's an example of how to handle push notifications using JavaScript:

self.addEventListener('push', (event) {
    var notificationData = event.data.json();
    var title = notificationData.title;
    var body = notificationData.body;

        self.registration.showNotification(title, {
            body: body,

In the above code, we listen for the ‘push' event and extract the title and body of the notification. We then use the Service Worker API to display the notification to the user.


In this article, we have learned how to implement FCM push notifications in an ASP.NET application. We covered the steps to set up Firebase Cloud Messaging, install the necessary NuGet package, initialize FirebaseApp, send push notifications from the server, and handle them on the client side. Push notifications can greatly enhance the user experience and keep users engaged with your application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents