在配置推送通知时存在一些问题。
setOnMessage() {
this.init();
this.checkPermission();
this.firebase.messaging().onMessage(function(payload) {
if (!("Notification" in window) || !("notification" in payload))
return false;
let data = payload.notification;
let options = {
body: data.body,
icon: '/icons/android-icon-48x48.png',
requireInteraction: true
};
let notification = new Notification(data.title, options);
setTimeout(() => {
notification.close();
}, 10000);
notification.onclick = () => {
//...
}
})
},
当当前浏览器窗口聚焦在宿主页面上时,此代码可以正常工作。但是,如果该窗口关闭(或什至集中在其他浏览器选项卡上),则显示默认通知(因此“ onclick”事件处理程序不起作用)。请帮忙,对不起我的英语。
答案 0 :(得分:1)
是的,本地服务器端“ firebase-messaging-sw.js”中有一个服务人员:
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/5.11.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.11.1/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
'messagingSenderId': '1234'
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
var data = payload.notification;
var options = {
body: data.body,
icon: '/icons/android-icon-48x48.png',
requireInteraction: true
};
return self.registration.showNotification(data.title, options);
});