firebase - Firebase sw.js 关闭浏览器后没有响应
问题描述
我创建了使用 Cloud Messaging 的 firebase 项目。到现在为止,当浏览器(chrome)打开时,我已经成功地从后端向客户端发送了一条消息,但是当我关闭浏览器通知时,直到重新打开浏览器才发送,您可以在下面的代码中看到我的 firebase-messaging-sw.js
importScripts('https://www.gstatic.com/firebasejs/5.0.4/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.0.4/firebase-messaging.js');
firebase.initializeApp({
messagingSenderId: "My-Sender-Id"
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
var notificationTitle = 'Background Message Title';
var notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
self.addEventListener('push', function(e) {
var data;
if (e.data) {
data = JSON.parse(e.data.text()).data;
} else {
data = {};
}
var title = data.title;
var options = {
body: data.body,
icon: 'assets/custom/img/logo.png',
vibrate: [100, 50, 100],
requireInteraction: true,
data: data.data ? data.data : null,
dir: "rtl",
actions: [{
action: data.action,
title: 'open',
icon: 'images/checkmark.png'
},
{
action: 'close',
title: 'close',
icon: 'images/xmark.png'
},
]
};
e.waitUntil(
self.registration.showNotification(title, options)
);
});
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
event.notification.close();
if (event.action != "" && event.action != "close") {
return clients.openWindow(event.action);
}
});
即使浏览器关闭,我如何才能收到通知?
解决方案
推荐阅读
- ios - Apple Pay 令牌的交易金额不正确
- html - 从 apache2 HTTPS 服务器(覆盆子 pi)实时流式传输视频的简单方法进化了吗?
- java - Spring Boot 应用程序:直接 url 不起作用
- c# - 获取 RootNode 的索引
- sql-server - SQL 将 CTE 生成的行转换为列 - 包括字段名
- python - 找不到 cv2 的模块名称
- r - Web 数据抓取问题-自动下载
- java - 如何阻止蓝牙扫描重复找到的设备以及如何使用 ScanFilter 限制扫描结果
- java - 如何在 Selenium Java Web Driver 中选择以下元素?
- sql - 如何使用 SQL 计算列中的特殊字符