1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
importScripts('https://www.gstatic.com/firebasejs/9.23.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.23.0/firebase-messaging-compat.js');
firebase.initializeApp({
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
appId: import.meta.env.VITE_FIREBASE_APP_ID,
});
const messaging = firebase.messaging();
// Handle notification clicks
self.addEventListener('notificationclick', function(event) {
console.log('[firebase-messaging-sw.js] Notification click received.', event);
event.notification.close();
const clickUrl = event.notification.data?.url || '/';
if (event.action) {
if (event.action === 'view') {
event.waitUntil(clients.openWindow(clickUrl));
}
return;
}
// Default click behavior
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(clientList => {
for (const client of clientList) {
if (client.url === clickUrl && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow(clickUrl);
}
})
);
});
// Handle notification close
self.addEventListener('notificationclose', function(event) {
console.log('[firebase-messaging-sw.js] Notification was closed', event);
});
// Install & activate
self.addEventListener('install', event => {
console.log('[firebase-messaging-sw.js] Service Worker installing.');
self.skipWaiting();
});
self.addEventListener('activate', event => {
console.log('[firebase-messaging-sw.js] Service Worker activated.');
event.waitUntil(clients.claim());
});
// Debugging push events
self.addEventListener('push', event => {
console.log('[firebase-messaging-sw.js] Push event received', event);
if (event.data) {
try {
const data = event.data.json();
console.log('[firebase-messaging-sw.js] Push data:', data);
} catch (e) {
console.log('[firebase-messaging-sw.js] Push data text:', event.data.text());
}
}
});
|