最近,我在我的angular js项目中实现了FCM。 有时,我从Chrome收到“您的网站已在后台更新”的通知,而不是实际的通知。
为什么会这样? 该问题的可能解决方法是什么?
if ('function' === typeof importScripts) {
importScripts('https://www.gstatic.com/firebasejs/5.5.3/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.5.3/firebase-messaging.js');
// TODO: fill in messaging sender id
firebase.initializeApp({
'messagingSenderId': 'MY SENDER ID'
});
const messaging = firebase.messaging();
// Installs service worker
self.addEventListener('install', (event) => {
console.log('Service worker installed');
});
self.addEventListener('notificationclick', (event) => {
// Event actions derived from event.notification.data from data received
var eventURL = event.notification.data;
event.notification.close();
if (event.action === 'confirmAttendance') {
clients.openWindow(eventURL.confirm);
} else {
clients.openWindow(eventURL.decline);
}
}, false);
messaging.setBackgroundMessageHandler((payload) => {
// Parses data received and sets accordingly
const data = JSON.parse(payload.data.notification);
const notificationTitle = data.title;
const notificationOptions = {
body: data.body,
// icon: '/static/images/5/icons/android-icon-96x96.png',
actions: [
{action: 'confirmAttendance', title: ' Confirm attendance'},
{action: 'cancel', title: ' Not coming'}
],
// For additional data to be sent to event listeners, needs to be set in this data {}
data: {confirm: data.confirm, decline: data.decline}
};
return self.registration.showNotification(notificationTitle, notificationOptions);
});
}