Web notification improvements
This commit is contained in:
parent
7ed3e80b9c
commit
13ef686e36
4 changed files with 61 additions and 46 deletions
22
public/sw.js
22
public/sw.js
|
|
@ -0,0 +1,22 @@
|
|||
// Notification click event listener
|
||||
self.addEventListener("notificationclick", (e) => {
|
||||
// Close the notification popout
|
||||
e.notification.close();
|
||||
// Get all the Window clients
|
||||
e.waitUntil(
|
||||
clients.matchAll({ type: "window" }).then((clientsArr) => {
|
||||
// If a Window tab matching the targeted URL already exists, focus that;
|
||||
const hadWindowToFocus = clientsArr.some((windowClient) =>
|
||||
windowClient.url === e.notification.data.url
|
||||
? (windowClient.focus(), true)
|
||||
: false,
|
||||
);
|
||||
|
||||
// Otherwise, open a new tab to the applicable URL and focus it.
|
||||
if (!hadWindowToFocus)
|
||||
clients
|
||||
.openWindow(e.notification.data.url)
|
||||
.then((windowClient) => (windowClient ? windowClient.focus() : null));
|
||||
}),
|
||||
);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue