php_nub_qq php_nub_qq - 2 months ago 29
Javascript Question

Service worker notification promise broken?

From MDN I see that

showNotification
returns a promise that should resolve to a
NotificationEvent
.


Syntax



​ServiceWorkerRegistration.showNotification(title,
[options]).then(function(NotificationEvent) { ... });

Returns



A Promise that resolves to a NotificationEvent.


However I have set it up here, the notification is being sent and all but if you look at the console you will notice that
event
is undefined.

navigator.serviceWorker.register('worker.js');

Notification.requestPermission(function (result) {
if (result === 'granted') {
navigator.serviceWorker.ready.then(function (registration) {
registration.showNotification('Laff', {
body: 'Hello, you have unread mesages!',
icon: '/apple-touch-icon.png',
tag: 'test'
}).then(function(event){
console.log(event);
});
});
}
});


I need to get a hold of the notification, which I thought I could do from
event.notification
but since
event
is undefined I don't really know what to do.

Am I doing something wrong?

Answer

I am not sure what exactly you mean of getting hold on notification ? if you are looking to capture an event when user will click on notification, you can do by adding listener.

add return .

return registration.showNotification('Laff', {

catch the event on notificationClick:

    self.addEventListener('notificationclick', function (event) {

        var tag = event;
}

hope it helps