-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserviceWorker.js
More file actions
37 lines (31 loc) · 971 Bytes
/
serviceWorker.js
File metadata and controls
37 lines (31 loc) · 971 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
27
28
29
30
31
32
33
34
35
36
self.addEventListener('push', function (event) {
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
const sendNotification = data => {
// you could refresh a notification badge here with postMessage API
var object = JSON.parse(data);
const title = object.title;
const icon = object.img;
const body = object.body;
const url = object.url;
return self.registration.showNotification(title, {
icon,
body,
data:{
url
}
});
};
if (event.data) {
const message = event.data.text();
event.waitUntil(sendNotification(message));
}
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
event.waitUntil(
clients.openWindow(event.notification.data.url)
);
// console.log(event.notification.data.url);
});