我有以下代码,基于
Google’s Documentation:
@H_502_2@var config = {
apiKey: "XX",authDomain: "XX",databaseURL: "XX",storageBucket: "XX",messagingSenderId: "XX"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
messaging.getToken()
.then(function(currentToken) {
if (currentToken) {
console.log(currentToken);
} else {
console.log('No Instance ID token available. Request permission to generate one.');
}
})
.catch(function(err) {
console.log('An error occurred while retrieving token. ',err);
});
})
.catch(function(err) {
console.log('Unable to get permission to notify. ',err);
});
奇怪的是,从不调用messaging.getToken()的then函数.控制台确实显示授予的通知权限,但在此之后,它保持安静(即我也没有收到任何错误).
难道我做错了什么?
编辑:应该注意的是,我正在尝试在Chrome扩展程序中实现此功能.
解决方法
我有同样的问题,我花了6个多小时来解决这个问题.您需要在主目录中创建虚拟“firebase-messaging-sw.js”(空文件)文件.
我的html页面位置是“http://localhost:56050/Notification/index.html”
我的firebase-messaging-sw.js是“http://localhost:56050/firebase-messaging-sw.js”