push notification works
Deploy app frontend / app frontend (push) Successful in 1m32s

This commit is contained in:
2024-12-05 21:06:57 +02:00
parent 23aced4071
commit d57e1569b3
8 changed files with 115 additions and 117 deletions
+48 -62
View File
@@ -1,13 +1,12 @@
const VER = 7
const VER = 14
self.addEventListener('message', async (event) => {
event.waitUntil(debug(event.source, Object.assign({ answer: true }, event.data)))
event.waitUntil(debug(Object.assign({ answer: true }, event.data)))
})
self.addEventListener('install', (event) => {
console.log('install event:', event)
// The promise that skipWaiting() returns can be safely ignored.
event.waitUntil(self.skipWaiting().then(() => debug(event.source, { installed: true })))
event.waitUntil(self.skipWaiting().then(() => debug({ installed: true })))
// Perform any other actions required for your
// service worker to install, potentially inside
@@ -21,23 +20,34 @@ self.addEventListener('activate', (event) => {
)
})
self.addEventListener('push', (event) => {
self.addEventListener('push', async (event) => {
debug({
event: 'push',
})
// PushData keys structure standart https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification
let pushData = event.data.json()
if (!pushData || !pushData.notification.title) {
console.error('Received WebPush with an empty title. Received body: ', pushData)
debug({
error: 'Received WebPush with an empty title. Received body',
})
}
event.waitUntil(
self.registration
.showNotification(pushData.notification.title, pushData.notification.options)
.then(function () {
// You can save to your analytics fact that push was shown
// fetch('https://your_backend_server.com/track_show?message_id=' + pushData.data.message_id);
}),
)
self.registration
.showNotification(pushData.notification.title, pushData.notification.options)
.then(function () {
// You can save to your analytics fact that push was shown
// fetch('https://your_backend_server.com/track_show?message_id=' + pushData.data.message_id);
})
.catch((error) => {
debug({
errorShowNotification: error.message,
})
})
})
self.addEventListener('notificationclick', (event) => {
debug({
event: 'notificationclick',
})
event.waitUntil(handleNotificationClick(event))
})
@@ -45,18 +55,13 @@ async function handleNotificationClick(event) {
event.notification.close()
if (!event.notification.data) {
await debug(null, {
await debug({
error: 'Click on WebPush with empty data, where url should be. Notification',
})
console.error(
'Click on WebPush with empty data, where url should be. Notification: ',
event.notification,
)
return
}
if (!event.notification.data.url) {
await debug(null, { error: 'Click on WebPush without url. Notification' })
console.error('Click on WebPush without url. Notification: ', event.notification)
await debug({ error: 'Click on WebPush without url. Notification' })
return
}
@@ -74,75 +79,56 @@ async function openURL(urlToOpen) {
for (const client of matchedClients) {
const clientUrlObj = new URL(client.url)
try {
await debug(client, {
urlToOpen,
'client.url': client.url,
})
if (
urlToOpenObj.pathname + urlToOpenObj.search !==
clientUrlObj.pathname + clientUrlObj.search
) {
await debug(client, {
urlToOpenObj: urlToOpenObj.pathname + urlToOpenObj.search,
clientUrlObj: clientUrlObj.pathname + clientUrlObj.search,
})
continue
}
if (client.focused) {
debug({ 'client.focused': true })
return
}
await client.focus()
debug({ 'client.focus()': true })
return
} catch (error) {
await debug({ error: error.message })
}
}
} catch (error) {
console.error('get client error', error)
debug({ 'get client error': error })
}
} catch (error) {
console.error('openURL error', error)
debug({ 'openURL error': error })
}
if (matchedClients && matchedClients.length) {
debug({ 'no matched clients': true })
}
try {
const client = await clients.openWindow(urlToOpen)
if (!client) {
await debug(client, { error: 'client is null' })
throw new Error('client is null')
await debug({ error: 'client is null' })
}
debug({ 'clients.openWindow()': true })
} catch (error) {
await debug(matchedClients && matchedClients[0], { error: error.message })
console.error(error)
}
}
async function debug(client, data) {
try {
if (!client) {
const matchedClients = await clients.matchAll({ type: 'window', includeUncontrolled: true })
for (const matchedClient of matchedClients) {
if (matchedClient.focused) {
client = matchedClient
break
}
}
if (!client && matchedClients.length) {
return Promise.all(matchedClients.map((client) => debug(client, data)))
}
}
if (!client) {
throw new Error('not found client for debug')
}
if (!client.postMessage) {
console.error('not found client.postMessage for debug', client)
return
}
await client.postMessage({
type: 'debug',
debug: Object.assign({ VER }, data || {}),
})
} catch (error) {
console.error(error)
}
}
function debug() {}
// async function debug(...args) {
// return fetch('https://hereconnect-debug.condev.ru/debug', {
// method: 'POST',
// body: JSON.stringify(args),
// })
// }