我再次在推送通知方面遇到一些麻烦
几周前相同的代码也有效。昨天我无意中发现推送通知不再起作用。我正在做的和写的一样文档 https://firebase.google.com/docs/cloud-messaging/js/receive.
我使用 NuxtJS 而不使用 @nuxtjs/messaging,因为它不支持最新的 Firebase 版本。
这是我的“firebase-messaging-sw.js”代码:
importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js')
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
apiKey: '###',
authDomain: '###',
databaseURL: '###',
projectId: '###',
storageBucket: '###',
messagingSenderId: '###',
appId: '###',
measurementId: '###',
})
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging()
let pushUrl = ''
messaging.onBackgroundMessage((payload) => {
console.log('Received background message ', payload)
// Customize notification here
const notificationTitle = payload.data.title
const notificationOptions = {
body: payload.data.body,
url: payload.data.url,
link: payload.data.url,
icon: './icon-for-push.png',
}
pushUrl = payload.data.url
self.registration.showNotification(notificationTitle, notificationOptions)
})
self.addEventListener('notificationclick', (e) => {
e.notification.close()
return self.clients.openWindow(pushUrl)
})
On the server side everything is okay:
Update 1第一次一切正常,X时间后出现问题。当我手动注册/取消注册服务人员时,它再次开始工作
Update 2我开始写日记来捕捉问题发生的时刻,然后我注意到推送通知在每次部署后停止工作