Skip to content

Commit

Permalink
Merge pull request #289 from k2maan/firebaseInitiFix
Browse files Browse the repository at this point in the history
Fixed: firebase project ID error on login due to missing configs in firebase-messaging-sw
  • Loading branch information
ravilodhi committed Oct 10, 2023
2 parents 4d180aa + 1fda567 commit 7ebec5f
Showing 1 changed file with 48 additions and 41 deletions.
89 changes: 48 additions & 41 deletions public/firebase-messaging-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,57 @@
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js');

const firebaseConfig = {apiKey: "", authDomain: "", databaseURL: "", projectId: "", storageBucket: "", messagingSenderId: "", appId: ""}
// wrapping the logic inside function and calling it as an IIFE
// to provide return statement support
(function () {
const firebaseConfig = { apiKey: "", authDomain: "", databaseURL: "", projectId: "", storageBucket: "", messagingSenderId: "", appId: "" }

// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp(firebaseConfig);
if (Object.values(firebaseConfig).some(value => !value)) {
return
}
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp(firebaseConfig);

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.onBackgroundMessage(payload => {
// Customize notification here
const notificationTitle = payload.data.title;
const notificationOptions = {
body: payload.data.body,
icon: "/img/icons/msapplication-icon-144x144.png",
data: {
click_action: "/notifications"
}
};
self.registration.showNotification(notificationTitle, notificationOptions);
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.onBackgroundMessage(payload => {
// Customize notification here
const notificationTitle = payload.data.title;
const notificationOptions = {
body: payload.data.body,
icon: "/img/icons/msapplication-icon-144x144.png",
data: {
click_action: "/notifications"
}
};
self.registration.showNotification(notificationTitle, notificationOptions);

// broadcast background message on FB_BG_MESSAGES so that app can receive that message
const broadcast = new BroadcastChannel('FB_BG_MESSAGES');
broadcast.postMessage(payload);
});
// broadcast background message on FB_BG_MESSAGES so that app can receive that message
const broadcast = new BroadcastChannel('FB_BG_MESSAGES');
broadcast.postMessage(payload);
});

self.addEventListener('notificationclick', event => {
event.notification.close();
const deepLink = event.notification.data.click_action;
event.waitUntil(
clients.matchAll({ includeUncontrolled: true, type: 'window' }).then(windowClients => {
// Check if the app window is already open
for (let client of windowClients) {
const clientPath = (new URL(client.url)).pathname;
if (clientPath === deepLink && 'focus' in client) {
return client.focus();
self.addEventListener('notificationclick', event => {
event.notification.close();
const deepLink = event.notification.data.click_action;
event.waitUntil(
clients.matchAll({ includeUncontrolled: true, type: 'window' }).then(windowClients => {
// Check if the app window is already open
for (let client of windowClients) {
const clientPath = (new URL(client.url)).pathname;
if (clientPath === deepLink && 'focus' in client) {
return client.focus();
}
}
}

// If the app window is not open, open a new one
if (clients.openWindow) {
return clients.openWindow(deepLink);
}
})
);
});
// If the app window is not open, open a new one
if (clients.openWindow) {
return clients.openWindow(deepLink);
}
})
);
});
})()

0 comments on commit 7ebec5f

Please sign in to comment.