wrbapp/frontend/public/firebase-messaging-sw.js

47 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-11-07 15:29:15 +01:00
self.addEventListener('notificationclick', (event) => {
console.log('On notification click: ', event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then((clientList) => {
for (const client of clientList) {
if (client.url === '/news' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/news');
}));
});
2022-09-30 21:17:12 +02:00
importScripts('https://www.gstatic.com/firebasejs/9.2.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.2.0/firebase-messaging-compat.js');
const firebaseConfig = {
apiKey: "AIzaSyCtHFyfCRkBt8MX5LPFogBi8ssKSypkW0g",
authDomain: "wrbapp.firebaseapp.com",
projectId: "wrbapp",
storageBucket: "wrbapp.appspot.com",
messagingSenderId: "160377508482",
appId: "1:160377508482:web:f651ccf2b242daf4879a9b",
measurementId: "G-31HEXDSVPZ"
};
const app = firebase.initializeApp(firebaseConfig);
2022-11-07 15:29:15 +01:00
const messaging = firebase.messaging();
messaging.onBackgroundMessage((payload) => {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: '/ios/256.png'
};
self.registration.showNotification(notificationTitle,
notificationOptions);
});