I am making a PWA. I need to save the token to firebase, but I get DOMException: Registration failed - push service error. The return messaging.getToken(); gives the error I mentioned. If I remove the return statement, I get error from firebase, because the token is undefined for obvious reasons.
Here is my code:
const messaging = firebase.messaging();
var perm;
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(() => {perm=true;});
}).catch(function (reason){
console.log(reason);
});
if(perm){
ChatRoom();
}
return null;
}
messaging.onMessage(function (payload){
console.log("payload:"+payload);
});
messaging.onTokenRefresh(function () {
messaging.getToken()
.then(function (newtoken) {
firestore.collection("tokens").doc(auth.currentUser.email).set({
mail: auth.currentUser.email,
token: newtoken
})//.then(() => {perm=true;console.log("New Token : "+ newtoken);});
})
.catch(function (reason) {
console.log(reason);
})
});
So my question is why can't I get my token, why do I get the error mentioned above? I press "accept" when it's asking for permission and I get the error afterwards.