From 21db3e15d1defdbf76b363ab77400c40344b8985 Mon Sep 17 00:00:00 2001 From: guptasiddharth Date: Tue, 3 Jun 2025 18:15:54 +0000 Subject: [PATCH 1/3] Add Firebase Messaging --- src/app.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/app.ts b/src/app.ts index 2cede1b..9367eaa 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,6 +7,7 @@ import { } from "firebase/vertexai"; +import { getMessaging, getToken, onMessage } from "firebase/messaging"; import { initializeApp } from "firebase/app"; @@ -204,6 +205,42 @@ async function fileToGenerativePart(file: Blob) { // --- Event Listener Setup --- document.addEventListener('DOMContentLoaded', () => { + // Add Firebase Messaging setup + const messaging = getMessaging(); + + // Request permission and get token + Notification.requestPermission().then((permission) => { + if (permission === 'granted') { + console.log('Notification permission granted.'); + // Get registration token. Initially this makes a network call. + // If the token is already cached it's retrieved from cache. + getToken(messaging, { vapidKey: '' }).then((currentToken) => { + if (currentToken) { + // Send the token to your server and update the UI if necessary + console.log('FCM token:', currentToken); + // You can now send this token to your backend to send notifications + } else { + // Show permission request UI + console.log('No registration token available. Request permission to generate one.'); + // TODO: Display some UI to the user to request permission to receive notifications. + } + }).catch((err) => { + console.log('An error occurred while retrieving token. ', err); + // TODO: Handle error. + }); + } else { + console.log('Unable to get permission to notify.'); + } + }); + + // Handle incoming messages + onMessage(messaging, (payload) => { + console.log('Message received. ', payload); + // Customize notification here + const notificationTitle = payload.notification?.title; + const notificationOptions = { body: payload.notification?.body, icon: '/firebase-logo.png' }; + new Notification(notificationTitle || 'New Message', notificationOptions); + }); const bTextOnlyInference = document.getElementById('bTextOnlyInference') as HTMLButtonElement; const bTextAndImageInference = document.getElementById('bTextAndImageInference') as HTMLButtonElement; From ad99aa386665f8645c8cb087efee4613d6035b32 Mon Sep 17 00:00:00 2001 From: guptasiddharth Date: Tue, 3 Jun 2025 18:19:39 +0000 Subject: [PATCH 2/3] Add VAPID key --- src/app.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 9367eaa..ae3b4d8 100644 --- a/src/app.ts +++ b/src/app.ts @@ -214,7 +214,7 @@ document.addEventListener('DOMContentLoaded', () => { console.log('Notification permission granted.'); // Get registration token. Initially this makes a network call. // If the token is already cached it's retrieved from cache. - getToken(messaging, { vapidKey: '' }).then((currentToken) => { + getToken(messaging, { vapidKey: 'BH2rCQkMjwmOTRH9jkjykkmx5xUx25cDweLmL6cTlbJznP_nxtivrycwO4ltmlX3TyiHQjGGjJtZJqADYefGtPE' }).then((currentToken) => { if (currentToken) { // Send the token to your server and update the UI if necessary console.log('FCM token:', currentToken); From 55a569de4d7da09773962e57d1208f6d188e1bb6 Mon Sep 17 00:00:00 2001 From: guptasiddharth Date: Tue, 3 Jun 2025 18:22:55 +0000 Subject: [PATCH 3/3] adding changes to firebase.json --- firebase.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/firebase.json b/firebase.json index 2c33c29..c87e0f3 100644 --- a/firebase.json +++ b/firebase.json @@ -7,6 +7,10 @@ "**/node_modules/**" ], "rewrites": [ + { + "source": "/firebase-messaging-sw.js", + "destination": "/firebase-messaging-sw.js" + }, { "source": "**", "destination": "/index.html"