diff --git a/src/utils/firebase.ts b/src/utils/firebase.ts index 7ca8626..26deade 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 {