简体   繁体   English

PWA Service Worker 通知点击

[英]PWA Service worker notification click

i'm trying do show a notification and do something when it's clicked.我正在尝试显示通知并在单击时执行某些操作。

    try {
    navigator.serviceWorker.getRegistration()
        .then(reg => {
            reg.showNotification("Guarda il videoclip!", {
                body: "clicca qua!",
                icon: "images/she_is_fire.png",
                vibrate: [100, 50, 100],
                tag: 'sample',
                actions: [{ action: 'explore', title: 'Guarda', icon: 'images/she_is_fire.png' }],
            });
            self.addEventListener('notificationclick', function(event) {
                event.notification.close();
                window.open("https://youtu.be/PAvHeRGZ_lA");
            }, false);
        })
        .catch(err => alert('Service Worker registration error: ' + err));

} catch (err) {
    alert('Notification API error: ' + err);
}

I added the eventlistener but it never gets fired.我添加了事件侦听器,但它从未被解雇。

What am i doing wrong?我做错了什么?

The correct place for handling clicks of Service Worker-based notifications is in Service Worker.处理基于 Service Worker 的通知点击的正确位置在 Service Worker 中。 You need to move your listener - the part with self.addEventListener('notificationclick', ...) - to your Service Worker code.您需要将您的侦听self.addEventListener('notificationclick', ...)带有self.addEventListener('notificationclick', ...)的部分self.addEventListener('notificationclick', ...)到您的 Service Worker 代码中。

Note that you don't have window access there.请注意,您在那里没有window访问权限。 To navigate to the URL, you'd need to use clients.openWindow instead.要导航到 URL,您需要改用clients.openWindow

So your app-side code will only have the reg.showNotification call and your handler in the Service Worker will look like this:因此,您的应用程序端代码将只有reg.showNotification调用,并且您在 Service Worker 中的处理程序将如下所示:

self.addEventListener('notificationclick', function (event) {
  event.notification.close();
  clients.openWindow("https://youtu.be/PAvHeRGZ_lA");
});

Always remember, after adding code ( refer answer ) in service-worker.js , restart your browser to reflect the changes.永远记住,在 service-worker.js 中添加代码(参考答案)后,重新启动浏览器以反映更改。 I was making change and not restarting browser window , got me frustrated !我正在进行更改而不是重新启动浏览器窗口,这让我很沮丧!

Just for complementary explanation, here is why is used self inside a service worker file according to Google Web Fundamentals:仅作为补充解释,以下是根据 Google Web Fundamentals 在服务工作者文件中使用self原因:

The weirdest bit of this code to most developers who are new to service workers is the self variable.对于大多数不熟悉 Service Worker 的开发人员来说,这段代码中最奇怪的一点是 self 变量。 self is commonly used in Web Workers, which a service worker is. self 常用在 Web Workers 中,也就是 service worker。 self refers to the global scope, kind of like window in a web page. self 指的是全局范围,有点像网页中的窗口。 But for web workers and service workers, self refers to the the worker itself.但是对于 web worker 和 service worker,self 指的是 worker 本身。

reference 参考

I am posting this explanation because helped me out understanding all these events including click.我发布这个解释是因为帮助我理解了所有这些事件,包括点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM