【发布时间】:2020-02-11 16:16:11
【问题描述】:
如果推送消息是使用 firebase.messaging().onMessage 发送的,我想在前台重新加载或触发某些事件,但它没有被触发。我正在使用带有后台通知的 firebase.mesaging.sw.js,它可以正常工作我的代码有什么问题?
firebase.js
const config = {
apiKey: "x",
projectId: "x",
storageBucket: "x",
messagingSenderId: "x"
};
firebase.initializeApp(config);
const msg = firebase.messaging()
msg.requestPermission()
.then(() => {
return msg.getToken()
})
.then((token) => {
})
.catch((err) => {
})
msg.onMessage(function(payload) {
alert("Foreground message fired!")
console.log(payload)
});
firebase.messaging.sw.js
importScripts("https://www.gstatic.com/firebasejs/7.0.0/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/7.0.0/firebase-messaging.js");
const config = {
apiKey: "x",
projectId: "x",
storageBucket: 'x',
messagingSenderId: "x"
};
firebase.initializeApp(config);
const msg = firebase.messaging()
msg.setBackgroundMessageHandler(function(payload) {
let options = {
body: payload.data.body,
icon: payload.data.icon
}
return self.registration.showNotification(payload.data.title, options);
});
我不知道我的代码有什么问题
【问题讨论】:
标签: javascript node.js laravel firebase firebase-cloud-messaging