My situation involves using FCM for sending web notifications. However, I am encountering a warning and the notifications are not functioning as expected when clicked (i.e., opening the notification URL). Below is my Service-Worker code:
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');
firebase.initializeApp({
'messagingSenderId': '<my senderid>'
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
self.addEventListener('notificationclick', function(event) {
event.notification.close();
var promise = new Promise(function(resolve) {
setTimeout(resolve, 1000);
}).then(function() {
return clients.openWindow(payload.data.locator);
});
event.waitUntil(promise);
});
var notificationTitle = payload.data.title;
var notificationOptions = {
body: payload.data.body,
icon: payload.data.icon
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
firebase-messaging-sw.js:108 Event handler of 'notificationclick' event must be added on the initial evaluation of worker script.
This is the warning message, with line 108 referring to this specific line:
self.addEventListener('notificationclick', function(event) {
It's worth noting that this issue occurs in Chrome version 55. Interestingly, Firefox does not encounter any problems and everything works fine. Thank you in advance.