我有一个网络工作者,我添加了'notificationclick'事件。 我想在同一个标签页中导航到网址,而不是打开新标签页。
任何线索如何做到这一点?
self.addEventListener('notificationclick', function (event) {
const id = event.notification.data.id;
let path = 'http://' + event.target.location.host;
if (event.notification.data.type === 'invite')
path += '/invite/' + id;
else
path += '/notification/' + id;
event.notification.close();
const promiseChain = clients.matchAll({
type: 'window',
includeUncontrolled: true
})
.then((windowClients) => {
let matchingClient = null;
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
if (windowClient.url === path) {
matchingClient = windowClient;
break;
}
}
if (matchingClient) {
return matchingClient.focus();
} else {
return clients.openWindow(path);
}
});
event.waitUntil(promiseChain);
});