简体   繁体   English

Firebase 消息服务工作者点击处理程序不起作用

[英]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.

相关问题 firebase 云消息传递给 A bad HTTP 响应代码 (404) 单击“允许”并手动添加服务工作者不起作用 - firebase cloud messaging gives A bad HTTP response code (404) after clicking on 'allow' and adding service worker manually doesn't work FCM消息来自服务工作者点击和图标的Chrome推送通知将不起作用 - FCM Messaging Chrome push notifications from service worker click and icon won't work Chrome上的Firebase Messaging服务人员 - Firebase Messaging service worker on chrome Firebase 消息传递和 Firebase 托管中的服务工作者错误 - Firebase Service worker error in Messaging and Firebase Hosting 具有file:// origin的服务工作者不起作用 - Service worker with file:// origin doesn't work Chrome中的Firebase Cloud Messaging Service Worker重定向 - Firebase Cloud Messaging Service Worker redirection in Chrome 为什么我的点击处理程序不起作用? - Why doesn't my click handler work? 为什么此点击处理程序不能在JQuery中工作? - Why doesn't this click handler work in JQuery? “this”在点击事件处理程序中不起作用 - "this" doesn't work in click event handler Workbox Service Worker 无法正常使用 PWA - Workbox service worker doesn't work as it should with PWA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM