Skip to content

Commit

Permalink
Improved: firebase code to run notification methods only in case of B…
Browse files Browse the repository at this point in the history
…opis app
  • Loading branch information
amansinghbais committed Jan 11, 2024
1 parent 488b200 commit 82e8b7c
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/utils/firebase.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 82e8b7c

Please sign in to comment.