在Nuxt.js/Vue.js中使用Firebase Messaging无法捕获notificationclick事件
问题:Firebase Messaging Service Worker无法捕获notificationclick事件
- 使用
@nuxtjs/firebase 8.2.2版本,已完成官方配置 firebase-messaging-sw.js中可正常捕获install、activate、push事件,通知能正常接收和显示,但点击通知时notificationclick事件处理程序完全无触发,service worker日志中无该事件记录- 尝试在account.vue页面通过
postMessage()向service worker传入用户邮箱,相关代码如下:
firebase-messaging-sw.js中的notificationclick事件代码
self.addEventListener('notificationclick', function (event) { console.log('Notification click'); event.notification.close(); const url = 'home'; event.waitUntil( self.clients.matchAll({ type: 'window' }).then((windowClients) => { // Check if there is already a window/tab open with the target URL for (let i = 0; i < windowClients.length; i++) { const client = windowClients[i]; // If so, just focus it. if (client.url === url && 'focus' in client) { console.log('focusing..'); return client.focus(); } } if (self.clients.openWindow) { console.log('open window'); } }) ); }, false);
account.vue中的postMessage代码
navigator.serviceWorker.ready.then((sw) => { console.log('Requesting permission. Service worker: ', sw); Notification.requestPermission().then(async (permission) => { let token = null; if (permission === 'granted') { console.log('Notification permission granted.'); await sw.active.postMessage({ action: 'saveEmail', email: this.email }); } }); });
firebase-messaging-sw.js中的message事件代码
self.addEventListener('message', async (msg) => { console.log('message', msg); const clients = await self.clients.matchAll({ type: 'window' }); for (const client of clients) { client.postMessage(msg); } if (msg.data.action === 'saveEmail') { USER_EMAIL = msg.data.email; } });
补充:已添加push事件处理程序,该事件可正常触发,但notificationclick事件仍无法捕获。
内容的提问来源于stack exchange,提问作者kiwi
相关产品推荐
相关产品推荐

