Skip to main content
This guide explains the process of configuring and using FCM with Novu, from getting your credentials to sending your first notification.
Novu uses FCM version V1

Step 1: Generate your service account key from Firebase

Get your project’s service account credentials from the Firebase Console.
  1. Log in to the Firebase console.
  2. Create a new Firebase project or select an existing project. Select Firebase Project
  3. Click the gear icon ⚙️ next to Project Overview.
  4. Select Project settings. Firebase Project Settings
  5. Click the Service accounts tab.
  6. Click the Generate new private key button. A confirmation menu appears. Firebase Service Accounts
  7. Click Generate key to download a JSON file containing your credentials. Firebase generate private key confirmation dialog
  8. Open the downloaded JSON file and ensure it contains these fields:
    {
      "type": "service_account",
      "project_id": "PROJECT_ID",
      "private_key_id": "PRIVATE_KEY_ID",
      "private_key": "PRIVATE_KEY",
      "client_email": "FIREBASE_ADMIN_SDK_EMAIL",
      "client_id": "CLIENT_ID",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "CLIENT_X509_CERT_URL"
    }
    

Step 2: Connect FCM to Novu

Add the credentials to your FCM integration in the Novu dashboard.
  1. Log in to the Novu dashboard.
  2. Navigate to the Integration Store.
  3. Click Connect provider.
  4. Click the Push tab, then select Firebase Cloud Messaging (FCM).
  5. Open the JSON file you downloaded from Firebase in Step 1.
  6. Copy the entire content of the JSON file and paste it into the Service Account field in the FCM integration modal. FCM integration
  7. Click Create Integration to save the integration.

Step 3: Register a subscriber’s device token

Before Novu can send a push notification to your subscriber, you must associate their device’s unique push token with their Novu subscriber profile. You can do this by making an API call to update the subscriber’s credentials.
import { Novu } from '@novu/api';
import { ChatOrPushProviderEnum } from "@novu/api/models/components";

const novu = new Novu({ secretKey: "<NOVU_SECRET_KEY>" });

await novu.subscribers.credentials.update(
  {
    providerId: ChatOrPushProviderEnum.Fcm,
    integrationIdentifier: "string",
    credentials: { deviceTokens: ["token1", "token2"] },
  },
  "subscriberId"
);
Novu automatically removes invalid device tokens from a subscribers’ profile and then sends the failure details to the MESSAGE_FAILED webhook.

Step 4: Send a notification

Now you’re ready to send a push notification. You can trigger a notification to a subscriber who has a registered device token.
import { Novu } from '@novu/api';

const novu = new Novu({ secretKey: "<NOVU_SECRET_KEY>" });

await novu.trigger({
  workflowId: "workflowId",
  to: { subscriberId: "subscriberId", },
  payload: {
    "key": "value"
},
  overrides: {
    "providers": {
        "fcm": {
            "topic": "topic-123"
        }
    }
},
});
Suppose you’re using the Firebase (FCM) provider to send push notifications to web browsers via Novu and want users to be returned to the website after clicking the notification. In that case, you must use the link property with a relative URL.
import { Novu } from '@novu/api';

const novu = new Novu({ secretKey: "<NOVU_SECRET_KEY>" });

await novu.trigger({
  workflowId: "workflowId",
  to: { subscriberId: "subscriberId", },
  payload: {
    "key": "value"
},
  overrides: {
    "providers": {
        "fcm": {
            "webPush": {
                "fcmOptions": {
                    "link": "/foo"
                }
            }
        }
    }
},
});

Frequently asked questions

As per Firebase pricing, Cloud Messaging product is free to use. If other Firebase products are used, then the cost are charged per the product.
You might come across an error such as: Sending message failed due to "The registration token is not a valid FCM registration token". This error happens because of an invalid or stale token. The fix for this is:
  1. Remove the old tokens.
  2. Generate a new token.
  3. Save the new token into user subscribers.
Try to generate a new token after clearing device cache and retry with this fresh token.
This error occurs when your token is no longer valid. To fix this, generate a new token and use it.
This error occurs if the FCM integration is active, but the subscriber is missing from the FCM credentials (deviceTokens). The credentials (deviceTokens) for the subscriber needs to be set.
Desktop notifications for websites can be sent using FCM webpush.