Kur*_*eko 7 javascript node.js laravel firebase firebase-cloud-messaging
如果推送消息是通过 firebase.messaging().onMessage 发送的,但它没有被触发,我想在foregrounf 中重新加载或触发某些事件。我正在使用带有后台通知的 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)
});
Run Code Online (Sandbox Code Playgroud)
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);
});
Run Code Online (Sandbox Code Playgroud)
我不知道我的代码有什么问题
对此的简单解决方案是将您的Firebse更新到最新版本。
例如。
importScripts('https://www.gstatic.com/firebasejs/7.8.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.8.0/firebase-messaging.js');
Run Code Online (Sandbox Code Playgroud)
注意:一旦你更新你的火力库版本则 messagingSenderId会在你不工作的火力点的消息,sw.js文件。您必须提供所有其他参数,例如。apiKey,专案编号,APPID连同messagingSenderId。
有关更多详细信息,您可以参考此解决方案
小智 5
2020年仍然有同样的问题。就我而言,情况是这样的:
importScripts后台消息和应用程序中为前台消息使用相同的版本firebaseApp.messaging().getToken().then((currentToken) => {
if (currentToken) {
console.log(currentToken)
} else {
// Show permission request.
console.log(
'No Instance ID token available. Request permission to generate one.')
}
/** When app is active */
firebase.messaging().onMessage((payload) => {
console.log(payload)
}, e => {
console.log(e)
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10526 次 |
| 最近记录: |