I need to show a SW notification when window cilent is hidden and should not show a notification when window client visibility state is visible.
I have tried below code. But SW shows default notification.
This site has been updated in the background
Could anyone please help me out.
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);
});