forked from Tucsky/aggr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
67 lines (57 loc) · 1.39 KB
/
sw.js
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
/* eslint-disable */
self.addEventListener('fetch', () => {})
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('airhorner').then(function(cache) {
return cache.addAll(global.serviceWorkerOption.assets)
})
)
})
self.addEventListener('push', event => {
const data = event.data.json()
const notification = {
body: data.body,
data: {
url: data.origin,
price: data.price,
market: data.market
}
}
self.registration.showNotification(data.title, notification)
self.clients
.matchAll({
type: 'window'
})
.then(function(clients) {
clients.forEach(function(client) {
client.postMessage({
price: data.price,
market: data.market
})
})
})
})
self.addEventListener('notificationclick', function(event) {
event.notification.close()
let url = 'https://charts.aggr.trade'
if (event.notification.data.url) {
url = event.notification.data.url
}
event.waitUntil(
clients
.matchAll({
type: 'window'
})
.then(function(clients) {
for (let i = 0; i < clients.length; i++) {
const client = clients[i]
if (client.url == url && 'focus' in client) {
return client.focus()
}
}
if (clients.openWindow) {
return clients.openWindow(url)
}
})
)
})