onmessgae event code --
messaging.onMessage(function(payload) {
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: payload.notification.icon,
data: payload.notification.click_action,
};
navigator.serviceWorker.register('firebase-messaging-sw.js');
if (Notification.permission === "granted") {
// If it's okay let's create a notification
navigator.serviceWorker.ready.then(function (registration) {
registration.showNotification(notificationTitle, notificationOptions);
})
}
})
my notification click event -
self.addEventListener('notificationclick', function (event) {
var default_val = 0;
console.log(event.notification)
event.notification.close();
try{
url = event.notification.data.FCM_MSG.notification.click_action
console.log("check1")
console.log("true")
}
catch (err) {
url = event.notification.data
console.log("check2")
}
//url = event.notification.data
if (default_val ==0) {
default_val = default_val+1
event.waitUntil(self.clients.openWindow(url));
}
})
my payload ---
{"notification":
{"body": "testing", "click_action": "/order1/", "icon": "http://localhost:8000/static/Logo/fcm_logo.jpg", "mutable-content": 1, "title": "hi "},
我正在为我的项目实施fcm服务,因为我在自己的标题中维护我在移动设备中的onmessage事件遇到了问题。我共享一些代码,我的setBackgroundMessageHandler事件对于移动版本或桌面版本都可以正常工作。 。但是在移动设备中出现消息事件问题