Incorporating Firebase Cloud Messaging into my project allowed me to send and receive push notifications successfully. While I can receive the push notifications, unfortunately, I am encountering issues with getting the notification events to function properly. Specifically, I am aiming to have the onMessage
event operational; however, I keep receiving an error message:
Messaging: This method is available in a Window context. (messaging/only-available-in-window).
This section showcases my service-worker code related to messaging:
// Granting the service worker access to Firebase Messaging.
// Only Firebase Messaging is accessible here as other Firebase libraries are not supported.
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-messaging.js')
// Initializing the Firebase app in the service worker by providing the
// messagingSenderId value.
firebase.initializeApp({
messagingSenderId: 'XXXXXXXX'
})
// Obtaining an instance of Firebase Messaging to manage background
// messages.
const messaging = firebase.messaging()
// messaging.setBackgroundMessageHandler(payload => {
// console.log(payload)
// })
messaging.onMessage(function(payload) {
// Messages received, either due to the
// application being active or
// because the notification was interacted with.
// `payload` will include your data.
console.log('Message received. ', payload)
})
I also attempted embedding the function within my Vue component, but the issue persists. Can someone please help me identify what may be causing this problem?