简体   繁体   中英

messaging.onBackgroundMessage is not a function

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) })

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM