我使用firebase向我的网站添加了Web push通知,该通知可以成功运行,但是问题是调用位于主js文件中的firebase的功能,因此当用户从网站打开许多标签时,它们会收到与打开标签的数量。 任何帮助或资源来解决此问题? 这是主要js中的代码
var config = {
apiKey: "xxxxxxxxxxx",
authDomain: "xxxxxxxxx",
databaseURL: "xxxxxxxx",
storageBucket: "xxxxxxxx",
messagingSenderId: "xxxxxxx",
appId: "xxxxxx",
projectId: "xxxxxxx"
};
if (!firebase.apps.length) {
firebase.initializeApp(config);
}
const messaging = firebase.messaging();
messaging.requestPermission()
.then(function(){
console.log("GRANTED");
console.log(messaging.getToken());
return messaging.getToken();
})
.then(function(token){
console.log("user token is "+ token);
})
.catch(function(err){
console.log('Error Occurred.' + err)
});
messaging.onMessage(function(payload){
title = payload.notification.title;
options={
body:payload.notification.body,
icon:payload.notification.icon,
click_action:payload.notification.click_action,
data: {
click_action:payload.notification.click_action
}
};
var notification = new Notification(title,options);
});