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.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
index.html
) in the Lovable project.<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>
firebaseConfig.ts
in your project’s source directory.
// firebaseConfig.ts
export const firebaseConfig = {
apiKey: "YOURAPIKEY",
authDomain: "YOURPROJECTID.firebaseapp.com",
projectId: "YOURPROJECTID",
storageBucket: "YOURPROJECTID.appspot.com",
messagingSenderId: "YOURMESSAGINGSENDER_ID",
appId: "YOURAPPID",
};
firebaseMessaging.ts
in your source directory.
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 };
firebase-messaging-sw.js
in the root directory of your project.
// 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);
});
// 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);
});
}
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();
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.