frigate/web/public/firebase-messaging-sw.js

37 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-07-10 21:17:36 +03:00
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
2024-07-19 23:03:06 +03:00
importScripts("https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js");
2024-07-10 21:17:36 +03:00
// 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
2024-07-19 23:32:20 +03:00
const fbConfig = await (
await fetch(`${window.location.href}/firebase-config.json`)
).json();
firebase.initializeApp(fbConfig);
2024-07-10 21:17:36 +03:00
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
2024-07-10 21:57:21 +03:00
const messaging = firebase.messaging();
2024-07-10 21:17:36 +03:00
2024-07-10 21:57:21 +03:00
messaging.onBackgroundMessage((payload) => {
console.log(
2024-07-19 23:03:06 +03:00
"[firebase-messaging-sw.js] Received background message ",
2024-07-10 21:57:21 +03:00
payload
);
// Customize notification here
2024-07-10 21:17:36 +03:00
const notificationOptions = {
2024-07-19 23:03:06 +03:00
body: payload.notification.body,
2024-07-19 23:32:20 +03:00
icon: payload.data.imageUrl,
2024-07-19 23:27:45 +03:00
tag: payload.data.id, // ensure that the notifications for same items are written over
2024-07-10 21:17:36 +03:00
};
2024-07-19 23:03:06 +03:00
self.registration.showNotification(
payload.notification.title,
notificationOptions
);
2024-07-10 21:17:36 +03:00
});