/lovable-integrations

Lovable and Firebase Cloud Messaging integration: Step-by-Step Guide 2025

Learn how to integrate Lovable with Firebase Cloud Messaging. Our step-by-step guide shows you how to set up, configure, and send targeted notifications seamlessly.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to integrate Lovable with Firebase Cloud Messaging?

 

Including the Firebase SDK in Your Lovable Project

 
  • Open your main HTML file (for example, index.html) in the Lovable project.
  • Add the following script tags within the <head> section to load Firebase libraries from the CDN. This replaces the usual terminal installation since Lovable does not have a terminal.

<!-- Firebase App (the core Firebase SDK) -->
<script src="https://www.gstatic.com/firebasejs/9.23.0/firebase-app-compat.js"></script>
<!-- Firebase Cloud Messaging -->
<script src="https://www.gstatic.com/firebasejs/9.23.0/firebase-messaging-compat.js"></script>
 

Setting Up Your Firebase Configuration

 
  • Create a new TypeScript file named firebaseConfig.ts in your project’s source directory.
  • This file will contain your Firebase project configuration. Replace the placeholder values with your Firebase project credentials.

// firebaseConfig.ts
export const firebaseConfig = {
  apiKey: "YOURAPIKEY",
  authDomain: "YOURPROJECTID.firebaseapp.com",
  projectId: "YOURPROJECTID",
  storageBucket: "YOURPROJECTID.appspot.com",
  messagingSenderId: "YOURMESSAGINGSENDER_ID",
  appId: "YOURAPPID",
};
 

Initializing Firebase and Firebase Cloud Messaging

 
  • Create a new TypeScript file named firebaseMessaging.ts in your source directory.
  • This file will initialize Firebase with your configuration and set up Firebase Cloud Messaging.

import firebase from "firebase/compat/app";
import "firebase/compat/messaging";
import { firebaseConfig } from "./firebaseConfig";

// Initialize Firebase app
const app = firebase.initializeApp(firebaseConfig);

// Initialize Firebase Cloud Messaging
const messaging = firebase.messaging();

// Optional: Handle background messages in the main code if needed
messaging.onMessage((payload) => {
console.log("Message received. ", payload);
});

export { messaging };


 

Creating a Firebase Messaging Service Worker

 
  • Create a new file named firebase-messaging-sw.js in the root directory of your project.
  • This service worker file is required to handle background notifications.

// firebase-messaging-sw.js

// Import Firebase scripts. Note: The Firebase CDN script paths match those in your index.html.
importScripts("https://www.gstatic.com/firebasejs/9.23.0/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/9.23.0/firebase-messaging-compat.js");

// Your Firebase configuration (it must match the one in firebaseConfig.ts)
const firebaseConfig = {
apiKey: "YOURAPIKEY",
authDomain: "YOURPROJECTID.firebaseapp.com",
projectId: "YOURPROJECTID",
storageBucket: "YOURPROJECTID.appspot.com",
messagingSenderId: "YOURMESSAGINGSENDER_ID",
appId: "YOURAPPID",
};

// Initialize the Firebase app in the service worker
firebase.initializeApp(firebaseConfig);

// Retrieve an instance of Firebase Messaging so that it can handle background messages.
const messaging = firebase.messaging();

messaging.onBackgroundMessage(function(payload) {
console.log("Received background message ", payload);
// Customize notification here as needed.
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: "/firebase-logo.png",
};
self.registration.showNotification(notificationTitle, notificationOptions);
});


 

Registering the Firebase Messaging Service Worker

 
  • In your main TypeScript/JavaScript file (or the entry file of your Lovable project), register the service worker.
  • Add the following code to the file where your application is initialized. This ensures that the service worker is registered when your app loads.

// In your main file (e.g., main.ts or app.ts)

// Check if service workers are supported and then register the one for Firebase Messaging
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("./firebase-messaging-sw.js")
.then((registration) => {
console.log("Service Worker registration successful with scope: ", registration.scope);
})
.catch((err) => {
console.error("Service Worker registration failed: ", err);
});
}


 

Requesting Notification Permission and Obtaining an FCM Token

 
  • Within your application (for example, in a component where you want to trigger notifications), request the user's permission to send notifications and obtain the FCM token.
  • Add the following snippet to a component or a dedicated file handling notifications. This code uses the messaging instance from your firebaseMessaging.ts file.

import { messaging } from "./firebaseMessaging";

// Request permission to send notifications
function requestNotificationPermission() {
console.log("Requesting notification permission...");

Notification.requestPermission().then((permission) => {
if (permission === "granted") {
console.log("Notification permission granted.");
// Get FCM token
messaging.getToken({ vapidKey: "YOURPUBLICVAPID_KEY" })
.then((currentToken) => {
if (currentToken) {
console.log("FCM Token:", currentToken);
// TODO: Send the token to your server for further use if required.
} else {
console.warn("No registration token available. Request permission to generate one.");
}
})
.catch((err) => {
console.error("An error occurred while retrieving token. ", err);
});
} else {
console.warn("Unable to get permission to notify.");
}
});
}

// Call the function when your app is ready to request permission
requestNotificationPermission();


 

Using the FCM Token in Your Application

 
  • After obtaining the token, you can use it according to your needs, such as sending it to your backend server for push notifications.
  • This completes the integration of Firebase Cloud Messaging in your Lovable project.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022