Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: firebase project ID error on login due to missing configs in firebase-messaging-sw #289

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 60 additions & 49 deletions public/firebase-messaging-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,69 @@ importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js')
const clickActionURL = '/notifications';
const iconURL = 'img/icons/msapplication-icon-144x144.png';

const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
}
function handleFirebaseInit() {
const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
}

// return if configurations are empty
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);
// 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: iconURL,
data: {
click_action: clickActionURL
}
};
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: iconURL,
data: {
click_action: clickActionURL
}
};
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);
}
})
);
});
}

// wrapping the logic inside function and calling it
// to provide return statement support
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
handleFirebaseInit()
initFirebase()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used an IIFE instead.

handleFirebaseInit()
Loading