mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-05-25 19:48:44 +09:00
31 lines
870 B
JavaScript
31 lines
870 B
JavaScript
self.addEventListener("push", async (event) => {
|
|
const data = await event.data.json()
|
|
event.waitUntil(Promise.all([ showNotification(data), updateBadgeCount(data.options) ]))
|
|
})
|
|
|
|
async function showNotification({ title, options }) {
|
|
return self.registration.showNotification(title, options)
|
|
}
|
|
|
|
async function updateBadgeCount({ data: { badge } }) {
|
|
return self.navigator.setAppBadge?.(badge || 0)
|
|
}
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close()
|
|
|
|
const url = new URL(event.notification.data.path, self.location.origin).href
|
|
event.waitUntil(openURL(url))
|
|
})
|
|
|
|
async function openURL(url) {
|
|
const clients = await self.clients.matchAll({ type: "window" })
|
|
const focused = clients.find((client) => client.focused)
|
|
|
|
if (focused) {
|
|
await focused.navigate(url)
|
|
} else {
|
|
await self.clients.openWindow(url)
|
|
}
|
|
}
|