【发布时间】:2020-01-13 00:48:05
【问题描述】:
我在我的项目中实现了 Firebase 云消息传递来发送和接收推送通知。我可以接收推送通知,但我无法让通知事件正常工作。具体来说,我想让onMessage 事件工作,但由于某种原因,它给了我这个错误:
消息传递:此方法在 Window 上下文中可用。 (消息传递/仅在窗口中可用)。
这是我的消息传递服务人员:
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-messaging.js')
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
messagingSenderId: 'XXXXXXXX'
})
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging()
// messaging.setBackgroundMessageHandler(payload => {
// console.log(payload)
// })
messaging.onMessage(function(payload) {
// Messages received. Either because the
// app is running in the foreground, or
// because the notification was clicked.
// `payload` will contain your data.
console.log('Message received. ', payload)
})
我也尝试将函数添加到我的 Vue 组件,但我仍然得到同样的错误。我做错了什么?
【问题讨论】:
标签: javascript firebase vue.js vuejs2 firebase-cloud-messaging