-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
26 lines (20 loc) · 819 Bytes
/
service-worker.js
File metadata and controls
26 lines (20 loc) · 819 Bytes
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
// service-worker.js
self.addEventListener('push', function (event) {
const data = event.data ? event.data.json() : {};
const title = data.title || "New Notification";
const options = {
body: data.body || "You have a new notification!",
icon: data.icon || "/icon.png", // Default icon
badge: data.badge || "/icon.png", // Default badge
};
// Show notification
event.waitUntil(self.registration.showNotification(title, options));
});
// Listen for notification click
self.addEventListener('notificationclick', function (event) {
event.notification.close(); // Close the notification
// Handle the click (for example, open the app or a specific URL)
event.waitUntil(
clients.openWindow(event.notification.data.url || '/')
);
});