messaging.onBackgroundMessage is not a function

Viewed 37

am getting error "messaging.onBackgroundMessage is not a function at firebase-messaging-sw.js:56:11"

Service worked is getting registered successfully but when I try to send notification, it getting delivered but not with the title and body. Am thinking that this error might be the reason for that.

Help me out in solving this. Thanks. Below is my service worker code :

importScripts('https://www.gstatic.com/firebasejs/3.5.0/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/3.5.0/firebase-messaging.js')

if ('serviceWorker' in navigator) {
  navigator.serviceWorker
    .register('../firebase-messaging-sw.js')
    .then(function (registration) {
      console.log('Registration successful, scope is:', registration.scope)
    })
    .catch(function (err) {
      console.log('Service worker registration failed, error:', err)
    })
}

firebase.initializeApp({
  messagingSenderId: '576646393071',
})

const messaging = firebase.messaging()

messaging.onBackgroundMessage((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',
  }

  self.registration.showNotification(notificationTitle, notificationOptions)
})

1 Answers

The onBackgroundMessage() was added in v7.18.0 of Firebase JS SDK. Try upgrading your Firebase SDK version to 8.10.0 or later as in the documentation.

importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js')
Related