I’ve a service employee that has a occasion listener for push messages.
Right here is the code
self.addEventListener('push', (occasion) => {
occasion.waitUntil(purchasers.matchAll({ sort: 'window' }).then((purchasers) => {
console.log('Push occasion 3', occasion.information.json())
for (let i = 0; i < purchasers.size; ++i) {
if (purchasers[i].visibilityState === 'seen') return // there's a seen window
}
// proceed with displaying a notification
const information = notification.information || {}
information.click_action = notification.click_action
return registration.showNotification(notification.title + " take a look at ", {
physique: notification.physique,
icon: notification.icon,
badge: notification.badge,
information: information,
requireInteraction: true,
actions: notification.actions,
picture: notification.picture,
click_action: notification.click_action
})
}))
})
async operate checkClientIsVisible() {
const windowClients = await purchasers.matchAll({
sort: 'window',
includeUncontrolled: true
})
for (var i = 0; i < windowClients.size; i++) {
if (windowClients[i].visibilityState === 'seen') {
return true
}
}
return false
}
The listener operate checks if there are any home windows/tabs displaying the web site within the scope of the sw.
It really works superb on desktops pwa (home windows/mac) and on android pwa too, however on IOS the push notification is displaying even when the pwa window is within the foreground, what I need is that the notifications DO NOT seem when the pwa is within the foreground, solely closed/background.
Thanks!