Below is my fcm setBackgroundMessageHandler
function:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
var event = new CustomEvent("name-of-event", payload);
// Dispatch/Trigger/Fire the event
document.dispatchEvent(event);
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
I am unable to access document
in above method. I have tried to save document in global variable and access it inside above method but it does not work.
var document = document;
.
.
.
document.dispatchEvent(event); // gives error: dispatchEvent of undefined