Reputation: 23
There is some problem in configure push notifications.
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 = () => {
//...
}
})
},
This code works fine when the current browser window is focused in on the host page. But if that window is closed (or even focused on other browser tab), then default notification shown (therefor the "onclick" event handler not working). Please help, sorry for my English.
Upvotes: 1
Views: 54
Reputation: 23
Yeah, there is a service worker in the local server side "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);
});
Upvotes: 1