首页 > 解决方案 > Firefox 没有在 Service Worker 中为推送消息打开窗口

问题描述

Firebase 云消息传递

我已经完成了所有设置,推送消息接收良好,当我点击它时,它会打开新窗口......但只有在 Chrome 中,在 Firefox 中它没有打开。

我特别允许弹出窗口,但没有任何区别。

我只是调试了1个小时

self.addEventListener('notificationclick', function(e) {
    console.log("This is printed to console fine");
    clients.openWindow('https://example.com'); // this makes no error, nothing
});

有任何想法吗?

适用于 Firefox 47.0

在 Firefox Quantum 60 中不起作用

订阅了一个错误。

标签: javascriptfirebasefirefoxweb-push

解决方案


我也为此苦苦挣扎了一段时间...

对于遇到问题的其他人,我想添加一些内容:

您仍然可以使用 firebase.messaging() 但您必须确保将其放在事件侦听器之后。

为了明确这一点:

这不起作用(clients.openWindow 什么都不做):

const messaging = firebase.messaging();

// Add an event listener to handle notification clicks
self.addEventListener('notificationclick', function (event) {
    if (event.action === 'close') {
        event.notification.close();
    } else if (event.notification.data.target_url && '' !== event.notification.data.target_url.trim()) {
        clients.openWindow(event.notification.data.target_url);
    }
});

messaging.setBackgroundMessageHandler(function (payload) {
    // ... add custom notification handling ...
});

这确实有效(clients.openWindow 按预期打开链接):

// Add an event listener to handle notification clicks
self.addEventListener('notificationclick', function (event) {
    if (event.action === 'close') {
        event.notification.close();
    } else if (event.notification.data.target_url && '' !== event.notification.data.target_url.trim()) {
        clients.openWindow(event.notification.data.target_url);
    }
});

const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(function (payload) {
    // ... add custom notification handling ...
});

仍然不确定根本原因。我还怀疑messaging() 搞砸了点击事件并让Firefox 拒绝打开一个窗口,因为它认为用户没有对通知采取直接行动。

但至少我有一个解决方法并且可以继续前进。

希望有帮助。


推荐阅读