From 82e8b7c1a5f0123c05c51d3bf1ec07f498a0351f Mon Sep 17 00:00:00 2001 From: amansinghbais Date: Thu, 11 Jan 2024 19:06:08 +0530 Subject: [PATCH] Improved: firebase code to run notification methods only in case of Bopis app --- src/utils/firebase.ts | 50 +++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/utils/firebase.ts b/src/utils/firebase.ts index 7ca8626a..26deade8 100644 --- a/src/utils/firebase.ts +++ b/src/utils/firebase.ts @@ -1,5 +1,7 @@ import { initializeApp } from "firebase/app"; import { getMessaging, getToken, onMessage } from "firebase/messaging"; +import { defineStore } from "pinia"; +let app; const initialiseFirebaseApp = async ( appFirebaseConfig: any, @@ -9,29 +11,35 @@ const initialiseFirebaseApp = async ( ) => { const firebaseConfig = appFirebaseConfig - const app = initializeApp(firebaseConfig); - const messaging = getMessaging(app); - const permission = await Notification.requestPermission(); + app = initializeApp(firebaseConfig); - if (permission === "granted") { - const token = await getToken(messaging, { - vapidKey: appFirebaseVapidKey - }); - await storeClientRegistrationToken(token) - - // handle foreground message - onMessage(messaging, (payload: any) => { - addNotification({ notification: payload, isForeground: true }); - }); - - // handle background message (service worker) - const broadcast = new BroadcastChannel('FB_BG_MESSAGES'); - broadcast.onmessage = (event) => { - addNotification({ notification: event.data, isForeground: false }); - }; - } else { - alert("You denied notifications."); + // Check for notifications required only in bopis app. + if(addNotification) { + const messaging = getMessaging(app); + const permission = await Notification.requestPermission(); + + if (permission === "granted") { + const token = await getToken(messaging, { + vapidKey: appFirebaseVapidKey + }); + await storeClientRegistrationToken(token) + + // handle foreground message + onMessage(messaging, (payload: any) => { + addNotification({ notification: payload, isForeground: true }); + }); + + // handle background message (service worker) + const broadcast = new BroadcastChannel('FB_BG_MESSAGES'); + broadcast.onmessage = (event) => { + addNotification({ notification: event.data, isForeground: false }); + }; + } else { + alert("You denied notifications."); + } } + console.log(app); + }; export {