I want my site to only allow the user to the next page if they accepted the notification request and their token are already uploaded to Firestore. I can't seem to figure out how to do it properly.
This is my code for asking for permission and uploading the token:
const messaging = firebase.messaging();
function InitializeFirebaseMessaging(){
messaging.requestPermission().then(function(){
console.log("Notification permission");
return messaging.getToken();
}).then(function(token){
console.log("Token: "+token);
firestore.collection("tokens").doc(auth.currentUser.email).set({
mail: auth.currentUser.email,
token: token
}).then(function(){
perm=true;
});
}).catch(function (reason){
console.log(reason);
});
}
messaging.onMessage(function (payload){
console.log(payload);
});
messaging.onTokenRefresh(function () {
messaging.getToken()
.then(function (newtoken) {
console.log("New Token : "+ newtoken);
})
.catch(function (reason) {
console.log(reason);
})
});
This is a piece of code I use to check if there is a token saved AND the current browser/pwa app accepted the notifications.
function ChatRoom(){
....
InitializeFirebaseMessaging();
.......
firestore.collection("tokens").doc(auth.currentUser.email).get().then((da)=> {
console.log("tokentest: " + da.data().token);
if (da.data().token!=null&&perm){
console.log("here??");
setSelfToken(true);
}
});
}
When I try to log in, it does not step to the next page even if I accepted the notifications. The token does not get uploaded to Firestore, and I keep getting this error:
DOMException: Registration failed - push service error
Any idea how could I fix it? Users must accept the notifications, and their saved tokens must be saved/updated.