Skip to content

Commit

Permalink
a possible fix for FF not focusing on a tab after notification click
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Feb 14, 2019
1 parent 1a17b01 commit e7d68b5
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,37 @@ self.addEventListener('notificationclick', event => {

const urlHash = '#/' + notification.data.topic;

const promises = self.clients.matchAll({
event.waitUntil(self.clients.matchAll({
type: 'window',
includeUncontrolled: true
}).then((windowClients) => {
let matchingClient = null;
let anyClient = null;
for (let i = 0; i < windowClients.length; i++) {
const url = new URL(windowClients[i].url);
if (url.hash.includes(notification.data.topic)) {
matchingClient = windowClients[i];
break;
// Found the Tinode tab with the right topic open.
return windowClients[i].focus();
} else {
// This will be the least recently used tab.
anyClient = windowClients[i];
}
}

// Found browser tab with the topic already open.
if (matchingClient) {
return matchingClient.focus();
}

// Found browser tab with Tinode on a different topic,
// Found tab with Tinode on a different topic,
// navigate to the right topic.
if (anyClient) {
const url = new URL(anyClient.url);
url.hash = '#/' + notification.data.topic;
return anyClient.navigate(url).then(() => { return anyClient.focus(); });
url.hash = urlHash;
return anyClient.focus().then(thisClient => {
return thisClient.navigate(url);
});
}

// Did not find a Tinode browser tab. Open one.
const url = new URL(self.location.origin);
url.hash = '#/' + notification.data.topic;
url.hash = urlHash;
return clients.openWindow(url);

});

event.waitUntil(promises);
}));
});

// This is needed for 'Add to Home Screen'.
Expand Down

0 comments on commit e7d68b5

Please sign in to comment.