I faced an issue where Notification.requestPermission
doesn't fire from my ServiceWorker
handler.
Interesting thing, that if try to use Notification.requestPermission
from browser console it works fine and I have see the notification request in top of the Chrome browser window.
by: Notification.requestPermission().then(res => console.log(res))
. But the same throw me an error during execution from ServiceWorker
file, like:
Uncaught TypeError: self.Notification.requestPermission is not a function
So, anyone maybe know what is wrong? I have already seen these posts on SoW:
But they does not solve my problem...
My SW push
part of code where I use notifications:
self.addEventListener('push', function(event) {
console.log(self.Notification, Notification.requestPermission)
self.Notification.requestPermission().then(res => console.log(res))
if (Notification.permission === 'denied') {
console.log('Permission wasn\'t granted. Allow a retry.');
return;
}
if (Notification.permission === 'default') {
console.log('The permission request was dismissed.');
return;
}
console.log('The permission request is granted!');
try {
event.waitUntil(
self.registration.showNotification(event && event.data && event.data.text() || 'Some Notification Here!')
);
} catch (e) {
throw new Error(`Error in SW: ${e}`)
}
})