I have this javascript class in which I have declared two properties and I have problems with the first one. I am trying to handle the tokens with Firebase in a project with node.js and react.js
export default class NoNotificationResource {
allTokens = [];
tokensLoaded = false;
constructor(messaging, database) {
this.database = database;
this.messaging = messaging;
try {
this.messaging.requestPermission().then(res => {
console.log('Permiso concedido');
}).catch(err => {
console.log('no acces', err);
});
} catch (err) {
console.log('NO hay soporte para notificaciones', err);
};
this.setupTokenRefresh();
this.database.ref('/fcmTokens').on('value', snapshot => {
this.allTokens = snapshot.val();
this.tokensLoaded = true;
})
};
setupTokenRefresh(){
this.messaging.onTokenRefresh(() => {
this.saveTokenToServer();
})
};
saveTokenToServer(){
this.messaging.getToken().then(res => {
if (this.tokensLoaded) {
const existingToken = this.findExistingToken(res);
if (existingToken) {
//Reemplazar token
} else {
//Crear nuevo token
}
}
});
}
findExistingToken(tokenToSave){
for(let tokenKey in this.allTokens){
const token = this.allTokens[tokenKey].token;
if (token === tokenToSave) {
return tokenKey;
}
}
return false;
}
}