a possible fix for FF not focusing on a tab after notification click
This commit is contained in:
+11
-17
@@ -34,43 +34,37 @@ self.addEventListener('notificationclick', event => {
|
|||||||
|
|
||||||
const urlHash = '#/' + notification.data.topic;
|
const urlHash = '#/' + notification.data.topic;
|
||||||
|
|
||||||
const promises = self.clients.matchAll({
|
event.waitUntil(self.clients.matchAll({
|
||||||
type: 'window',
|
type: 'window',
|
||||||
includeUncontrolled: true
|
includeUncontrolled: true
|
||||||
}).then((windowClients) => {
|
}).then((windowClients) => {
|
||||||
let matchingClient = null;
|
|
||||||
let anyClient = null;
|
let anyClient = null;
|
||||||
for (let i = 0; i < windowClients.length; i++) {
|
for (let i = 0; i < windowClients.length; i++) {
|
||||||
const url = new URL(windowClients[i].url);
|
const url = new URL(windowClients[i].url);
|
||||||
if (url.hash.includes(notification.data.topic)) {
|
if (url.hash.includes(notification.data.topic)) {
|
||||||
matchingClient = windowClients[i];
|
// Found the Tinode tab with the right topic open.
|
||||||
break;
|
return windowClients[i].focus();
|
||||||
} else {
|
} else {
|
||||||
|
// This will be the least recently used tab.
|
||||||
anyClient = windowClients[i];
|
anyClient = windowClients[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Found browser tab with the topic already open.
|
// Found tab with Tinode on a different topic,
|
||||||
if (matchingClient) {
|
|
||||||
return matchingClient.focus();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Found browser tab with Tinode on a different topic,
|
|
||||||
// navigate to the right topic.
|
// navigate to the right topic.
|
||||||
if (anyClient) {
|
if (anyClient) {
|
||||||
const url = new URL(anyClient.url);
|
const url = new URL(anyClient.url);
|
||||||
url.hash = '#/' + notification.data.topic;
|
url.hash = urlHash;
|
||||||
return anyClient.navigate(url).then(() => { return anyClient.focus(); });
|
return anyClient.focus().then(thisClient => {
|
||||||
|
return thisClient.navigate(url);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Did not find a Tinode browser tab. Open one.
|
// Did not find a Tinode browser tab. Open one.
|
||||||
const url = new URL(self.location.origin);
|
const url = new URL(self.location.origin);
|
||||||
url.hash = '#/' + notification.data.topic;
|
url.hash = urlHash;
|
||||||
return clients.openWindow(url);
|
return clients.openWindow(url);
|
||||||
|
}));
|
||||||
});
|
|
||||||
|
|
||||||
event.waitUntil(promises);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// This is needed for 'Add to Home Screen'.
|
// This is needed for 'Add to Home Screen'.
|
||||||
|
|||||||
Reference in New Issue
Block a user