[英]Firebase messaging service worker click handler doesn't work
I am using FCM to handle push notifications, but notification is coming in data
key instead of notification
.我正在使用 FCM 来处理推送通知,但通知来自
data
key 而不是notification
。
Here is the code I am using to handle new message and show as notification.这是我用来处理新消息并显示为通知的代码。 I am seeing notification with all options and data as I specified.
我看到了我指定的所有选项和数据的通知。 However, when I click notification, it doesn't fire any "notificationclick" event.
但是,当我单击通知时,它不会触发任何“notificationclick”事件。 It also doesn't print
event.notification
.它也不打印
event.notification
。
importScripts('https://www.gstatic.com/firebasejs/5.3.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.3.1/firebase-messaging.js');
firebase.initializeApp({
messagingSenderId: "SOME_ID",
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(payload => {
console.log(payload);
const options = {
body: payload.data.body,
icon: payload.data.icon,
click_action: payload.data.click_action,
link: payload.data.link,
data: {
time: new Date(Date.now()).toString(),
click_action: payload.data.click_action,
},
};
self.registration.showNotification(payload.data.title, options);
});
self.addEventListener("notificationclick", function(event) {
console.log(event.notification);
const clickedNotification = event.notification;
clickedNotification.close();
const urlToOpen = clickedNotification.data && clickedNotification.data.click_action;
const promiseChain = clients.matchAll({
type: 'window',
includeUncontrolled: true,
})
.then((windowClients) => {
let matchingClient = null;
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
if (windowClient.url === urlToOpen) {
matchingClient = windowClient;
break;
}
}
if (matchingClient) {
return matchingClient.focus();
} else {
return clients.openWindow(urlToOpen);
}
});
event.waitUntil(promiseChain);
});
1 you can check with your firebase-messaging-sw.js file in the application whether the recent changes are in that file after the build. 1 您可以使用应用程序中的 firebase-messaging-sw.js 文件检查构建后该文件中是否有最近的更改。
2 we only need the most recent changes in firebase-messaging-sw.js, so need to check and most recent updated firebase-messaging-sw.js file and unregister all older firebase-messaging-sw.js. 2 我们只需要 firebase-messaging-sw.js 中的最新更改,因此需要检查和最新更新的 firebase-messaging-sw.js 文件并取消注册所有旧的 firebase-messaging-sw.js。
it worked for me.它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.