-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
service-worker.js.template
56 lines (45 loc) · 1.25 KB
/
service-worker.js.template
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
self.addEventListener('install', event => {
self.skipWaiting()
console.log('worker installed', event)
})
self.addEventListener('activate', event => {
console.log('worker activated', event)
})
self.addEventListener('push', event => {
console.log('push fired', event)
const data = event.data.json()
const options = {
body: data.body,
icon: data.icon ? data.icon : '',
image: data.image ? data.image : '',
data: {
url: data.url
}
}
if (data.actions)
options.actions = data.actions
event.waitUntil(
Promise.all([
self.registration.showNotification(data.title, options)
])
)
})
self.addEventListener('notificationclick', event => {
console.log('notificationclick fired', event)
// INFO: You can customize this callback for your needs
// currently we are closing notification on click
event.notification.close()
if (event.action) {
console.log('action clicked', event.action)
}
// then if url is provided open a new window
var clickResponsePromise = Promise.resolve()
if (event.notification.data && event.notification.data.url) {
clickResponsePromise = clients.openWindow(event.notification.data.url)
}
event.waitUntil(
Promise.all([
clickResponsePromise
])
)
})