我需要在隐藏窗口cilent时显示SW通知,并且在窗口客户端可见性状态可见时不应显示通知。
我试过下面的代码。但SW显示默认通知。
此网站已在后台更新
有人可以帮帮我。
self.addEventListener('push', function(event) {
if (event.data) {
console.log('This push event has data: ',event.data.text());
} else {
console.log('This push event has no data.');
}
// isClientFocused will return true if any of the window client is visible.
const promiseChain = isClientFocused()
.then(function(clientIsVisible){
if (clientIsVisible) {
return self.registration.showNotification('Dummy')
.then(function(){
self.registration.getNotifications().then(function(notifications)
{
notifications.forEach(function(notification){
notification.close();
})
})
});
}
//Client is not focused.So need to show notification.
else{
const title = 'POC Demo';
const options = {
body: event.data.text(),
tag: 'data-notification',
data: {
message: event.data.text()
}
};
return self.registration.showNotification(title,options);
}
});
event.waitUntil(promiseChain);
});