-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
56 lines (51 loc) · 1.76 KB
/
sw.js
File metadata and controls
56 lines (51 loc) · 1.76 KB
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
// PolyMind Service Worker — notification support
// This SW enables push notifications for live alerts
self.addEventListener('install', function(e) {
self.skipWaiting();
});
self.addEventListener('activate', function(e) {
e.waitUntil(self.clients.claim());
});
// Bridge from page context — the page posts { type:'NOTIFY', ... } and the SW
// shows the OS-level notification via self.registration.
self.addEventListener('message', function(e) {
var data = e.data || {};
if (data.type !== 'NOTIFY') return;
var title = data.title || 'PolyMind Alert';
var options = {
body: data.body || 'New signal detected',
icon: '/icon-192.png',
badge: '/icon-192.png',
tag: data.tag || 'polymind-alert',
renotify: true,
data: { alertId: data.alertId || null },
};
try { self.registration.showNotification(title, options); } catch (err) {}
});
// Handle notification clicks — focus or open the app
self.addEventListener('notificationclick', function(e) {
e.notification.close();
e.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function(clients) {
for (var i = 0; i < clients.length; i++) {
if (clients[i].url.includes(self.location.origin) && 'focus' in clients[i]) {
return clients[i].focus();
}
}
return self.clients.openWindow('/');
})
);
});
// Listen for push events (future use)
self.addEventListener('push', function(e) {
var data = e.data ? e.data.json() : {};
var title = data.title || 'PolyMind Alert';
var options = {
body: data.body || 'New signal detected',
icon: '/icon-192.png',
badge: '/icon-192.png',
tag: data.tag || 'polymind-alert',
renotify: true
};
e.waitUntil(self.registration.showNotification(title, options));
});