basic support for push messages with actions

This commit is contained in:
Dobromir Popov
2024-05-07 11:31:59 +03:00
parent d8c11915fa
commit a8a50c76a8
3 changed files with 82 additions and 21 deletions

View File

@ -21,18 +21,39 @@ self.addEventListener('push', function (event) {
return
}
const data = JSON.parse(event.data.text())
console.log('SW: Push data', data)
actions: [
//font awesome icons
{ action: 'accept', title: 'Accept', icon: 'fa fa-check' },
{ action: 'decline', title: 'Decline', icon: 'fa fa-times' }
]
event.waitUntil(
registration.showNotification(data.title, {
body: data.message,
icon: '/favicon.ico',
actions: data.actions,
actions: [...data.actions, { action: 'close', title: 'Close', icon: 'fa fa-times' }],
data: data.url,
})
)
})
self.addEventListener('notificationclick', function (event) {
console.log('Notification click: tag', event.notification.tag)
console.log('Notification click: tag', event.notification.tag, 'action', event.action)
event.notification.close()
switch (event.action) {
case 'accept':
console.log('User accepted the action.');
// handle acceptance
break;
case 'decline':
console.log('User declined the action.');
// handle decline
break;
default:
// handle other cases
break;
}
console.log(event)
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function (clientList) {
if (clientList.length > 0) {