-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
Copy pathweb-push-notifications.ts
69 lines (56 loc) · 2.11 KB
/
web-push-notifications.ts
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
68
69
/// <reference lib="WebWorker" />
/// <reference types="vite/client" />
import { ELK_PAGE_LIFECYCLE_FROZEN } from '../constants'
import {
closeDatabaseConnections,
createNotificationOptions,
findNotification,
} from './notification'
import type { PushPayload } from '~/service-worker/types'
declare const self: ServiceWorkerGlobalScope
self.addEventListener('message', (event) => {
if (event.data === ELK_PAGE_LIFECYCLE_FROZEN)
closeDatabaseConnections()
})
export function onPush(event: PushEvent) {
const promise = isClientFocused().then((isFocused) => {
if (isFocused)
return Promise.resolve()
const options: PushPayload = event.data!.json()
return findNotification(options)
.catch((e) => {
console.error('unhandled error finding notification', e)
return Promise.resolve(undefined)
})
.then((notificationInfo) => {
return self.registration.showNotification(options.title, createNotificationOptions(options, notificationInfo))
})
})
event.waitUntil(promise)
}
export function onNotificationClick(event: NotificationEvent) {
const reactToNotificationClick = new Promise((resolve) => {
event.notification.close()
resolve(openUrl(event.notification.data.url))
})
event.waitUntil(reactToNotificationClick)
}
function findBestClient(clients: WindowClient[]) {
const focusedClient = clients.find(client => client.focused)
const visibleClient = clients.find(client => client.visibilityState === 'visible')
return focusedClient || visibleClient || clients[0]
}
async function openUrl(url: string) {
const clients = await self.clients.matchAll({ type: 'window' })
// Chrome 42-48 does not support navigate
if (clients.length !== 0 && 'navigate' in clients[0]) {
const client = findBestClient(clients as WindowClient[])
await client.navigate(url).then(client => client?.focus())
}
await self.clients.openWindow(url)
}
function isClientFocused() {
return self.clients
.matchAll({ type: 'window', includeUncontrolled: true })
.then(windowClients => Promise.resolve(windowClients.some(windowClient => windowClient.focused)))
}