forked from Tucsky/aggr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
64 lines (54 loc) · 1.33 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
/* eslint-disable */
self.addEventListener('fetch', event => {})
self.addEventListener('push', event => {
const data = event.data.json()
const notification = {
body: data.body,
data: {
url: data.origin,
price: data.price,
market: data.market,
direction: data.direction,
message: data.message
}
}
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,
direction: data.direction,
message: data.message
})
})
})
})
self.addEventListener('notificationclick', function (event) {
event.notification.close()
let url = 'https://aggr.trade'
if (event.notification.data.url) {
url = event.notification.data.url
}
event.waitUntil(
clients
.matchAll({
type: 'window'
})
.then(function (clientsArr) {
for (let i = 0; i < clientsArr.length; i++) {
const client = clientsArr[i]
if (client.url == url && 'focus' in client) {
return client.focus()
}
}
if (clients.openWindow) {
return clients.openWindow(url)
}
})
)
})