K
K
karpo5182020-02-05 12:03:57
JavaScript
karpo518, 2020-02-05 12:03:57

How to set url on click on push notification from web worker?

There is a small web worker that handles push notifications from the server.

web worker code
self.addEventListener('push', function (event) {
    if (!(self.Notification && self.Notification.permission === 'granted')) {
        return;
    }

    const sendNotification = body => {

        const title = "Web Push example";
        return self.registration.showNotification(title, {
            body,
        });
    };

    if (event.data) {
        let message = event.data.json();
        event.waitUntil(sendNotification(message.body));
    }
});

I pass json into it, which includes the title, the body of the notification and the url that needs to be opened by clicking on the notification. How can I adjust the web worker code so that when a notification is clicked, the url message.click_url opens?

I found examples without processing requests to the server. For example https://gist.github.com/deanhume/c0d29d40e0c77dfbab8f
but they didn't work for me in firefox. And it is not clear how to connect 2 events.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question