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
订阅了一个错误。
答案 0 :(得分:2)
我从服务工作者那里删除了:
const messaging = firebase.messaging();
它现在正在运作。
这只是坚果。
答案 1 :(得分:1)
我也为此苦了一段时间...
对于其他有问题的人,我想添加一些内容:
您仍然可以使用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 ...
});
仍然不确定根本原因。我还怀疑Messenger()弄乱了click事件,并导致Firefox拒绝打开窗口,因为它认为用户未对通知采取任何直接操作。
但是至少我有一个解决方法,可以继续。
希望有帮助。
答案 2 :(得分:0)
找出原因。
仅当您这样发送(从服务器)消息传递有效负载时,Firebase.messaging
才是罪魁祸首:
{
"notification": {
//...
}
}
notification
属性的存在将阻止notificationclick
由于Firebase SDK中的某些原因而传播。
您可以改为发送以下内容
{
"data": {
//...
}
}