javascript之Firefox 未在服务 worker 中为推送消息打开窗口

arxive 阅读:43 2025-06-02 22:19:02 评论:0

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

订阅了一个错误。

请您参考如下方法:

我也纠结过一段时间...

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

您仍然可以使用 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 拒绝打开窗口,因为它认为用户没有对通知采取直接操作。

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

希望对您有所帮助。


标签:JavaScript
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号