49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
importScripts('https://www.gstatic.com/firebasejs/10.7.1/firebase-app-compat.js');
|
|
importScripts('https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-compat.js');
|
|
|
|
firebase.initializeApp({
|
|
apiKey: "AIzaSyAw1v4COZ68Po8CuwVKrQq0ygf7zFd2QCA",
|
|
authDomain: "fcmtest-push.firebaseapp.com",
|
|
projectId: "fcmtest-push",
|
|
storageBucket: "fcmtest-push.firebasestorage.app",
|
|
messagingSenderId: "439263996034",
|
|
appId: "1:439263996034:web:9b3d52af2c402e65fdec9b"
|
|
});
|
|
|
|
const messaging = firebase.messaging();
|
|
|
|
messaging.onBackgroundMessage(function(payload) {
|
|
console.log('Received background message:', payload);
|
|
|
|
const notificationTitle = payload.notification.title;
|
|
const notificationOptions = {
|
|
body: payload.notification.body,
|
|
icon: '/icon-192.png',
|
|
badge: '/icon-192.png',
|
|
tag: 'fcm-test',
|
|
data: payload.data
|
|
};
|
|
|
|
self.registration.showNotification(notificationTitle, notificationOptions);
|
|
});
|
|
|
|
self.addEventListener('notificationclick', function(event) {
|
|
console.log('Notification clicked:', event);
|
|
event.notification.close();
|
|
|
|
if (event.action === 'close') return;
|
|
|
|
event.waitUntil(
|
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function(clientList) {
|
|
for (const client of clientList) {
|
|
if (client.url === '/' && 'focus' in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
if (clients.openWindow) {
|
|
return clients.openWindow('/');
|
|
}
|
|
})
|
|
);
|
|
});
|