当我们在Windows 10通知历史记录中有2个以上的推送通知时,则在单击chrome时,浏览器中只会打开最新的通知。但是在Firefox和Edge浏览器中,所有通知都在浏览器中打开。
这是我的服务人员代码。 推送事件:
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
var data = {};
if (event.data) {
data = event.data.json();
} else {
console.log('received empty data for push notification');
}
let notificationTitle = data.title;
const notificationOptions = {
body: data.bodyText,
icon: data.imageUrl,
sticky:false,
notificationCloseEvent:false,
data: {
url: data.conversationUrl,
},
};
const message = {notificationTitle, notificationOptions};
const promise = isClientFocused()
.then((clientIsFocused) => {
if (clientIsFocused) {
console.log('App is in focus no need to show notification');
return;
} else {
return firstWindowClient().then((windowClient) => {
console.log('URL is already in open');
windowClient.postMessage(message);
}, () => {
console.log('Url is not in open.');
return self.registration.showNotification(notificationTitle, notificationOptions);
});
}
});
event.waitUntil(promise);
});
通知点击事件:
self.addEventListener('notificationclick', (event) => {
const urlToOpen = new URL(event.notification.data.url, self.location.origin).href;
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 === urlToOpen) {
matchingClient = windowClient;
break;
}
}
if (matchingClient) {
return matchingClient.focus();
} else {
return clients.openWindow(urlToOpen);
}
});
event.waitUntil(promiseChain);
});