我试图在我的angular项目中实现fcm云消息,我设法注册令牌并请求通知权限。但是我没有收到消息。
firebase-messaging-sw
importScripts("https://www.gstatic.com/firebasejs/9.15.0/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/9.15.0/firebase-messaging-compat.js");
firebase.initializeApp({
apiKey: "XXXXXXXXXX",
authDomain: "XXXXXXXXXX.firebaseapp.com",
databaseURL: "https://XXXXXXXXXX.firebaseio.com",
projectId: "XXXXXXXXXX",
storageBucket: "XXXXXXXXXX.appspot.com",
messagingSenderId: "XXXXXXXXXX",
appId: "XXXXXXXXXX",
measurementId: "G-XXXXXXXXXX"
});
const messaging = firebase.messaging();
console.log("service worker", messaging);
角。JSON
"assets"("src/favicon.ico","src/assets","src/firebase-messaging-sw.js","src/manifest.json"),
app.module
initializeApp(environment.firebase);
组件
requestPermission() {
const messaging = getMessaging();
getToken(messaging,
{ vapidKey: environment.firebase.vapidKey}).then(
(currentToken) => {
if (currentToken) {
console.log("token");
console.log(currentToken);
} else {
console.log('No registration token available. Request permission to generate one.');
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
});
}
listen() {
console.log("listen start");
const messaging = getMessaging();
onMessage(messaging, (payload) => {
console.log('Message received. ', payload);
});
}
CALL INSOMNIA (https://fcm.googleapis.com/fcm/send)
{
"notification" : {
"title" : "test",
"body": "test"
},
"to":"xxxxxx"
}
你能确认你的FCM service worker已经在你的浏览器中注册了吗?(例如:如果你运行在localhost:4200,启动inspect ->应用程序→服务人员)
如果已注册,则检查FCM配置。
尝试在你的firebase-messaging.js中使用命名空间导入
请让我知道它解决了你的问题!谢谢! !