self.registration.showNotification 中data是可以传额外的参数 。
有个细节,官方没有提到的,需要用setTimeout包着showNotification,Chrome推送出的消息才不会出现链接无法点击的问题 。
监听推送消息的点击事件
// 推送消息点击事件self.addEventListener('notificationclick', event => { console.log('notificationclick'); const clickedNotification = event.notification; const urlToOpen = new URL(clickedNotification.data.href, self.location.origin).href; let promiseChain = clients.matchAll({ type: 'window', includeUncontrolled: true }).then(windowClients => { let matchingClient = null; for (let i = 0, max = windowClients.length; i < max; i++) { let windowClient = windowClients[i]; if (windowClient.url.split('?')[0] === urlToOpen.split('?')[0]) { matchingClient = windowClient; break; } } return matchingClient ? matchingClient.focus() : clients.openWindow(urlToOpen); }); event.waitUntil(promiseChain); clickedNotification.close();});监听 notificationclick 点击事件,除了需要打开弹窗,还要判断该弹窗是否曾经打开过,如果是则只需active tab即可 。
参考链接
https://www.boatsky.com/blog/66
推荐阅读
- 8 个提高 JavaScript 性能的方法
- 教你用这十种茶帮你轻松消除各种不适
- 喝茶可以预防中风 让自己更年轻哦
- 如何在家轻松练瑜伽
- 对标苹果iPhoneSE:小米高管确认轻薄单手小屏新机
- 车联网:未来5G核心应用场景之一,产业链趋于成熟,附相关个股
- 韩式减肥茶 让你轻松瘦身
- 保持年轻的运动有哪些
- 茶叶在食品上的应用
- 详解Spring MVC 构建入门级 Web 应用程序