I have created one firebase notification Web application.When I login from web application then I generate FCM token and send it to my API server this is working properly.I have written onTokenRefresh method in my code but I am not able to test this code. Is there any way to test this code?
firebase.initializeApp(config);
messaging = firebase.messaging();
// Get Instance ID token. Initially, this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
messaging.requestPermission().then(function() {
return messaging.getToken();
}).then(function(currentToken) {
if (currentToken) {
sendTokenToServer(currentToken);
updateUIForPushEnabled(currentToken);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
// Show permission UI.
updateUIForPushPermissionRequired();
setTokenSentToServer(false);
}
})
.catch(function(err) {
console.log('An error occurred while retrieving token. ', err);
showToken('Error retrieving Instance ID token. ', err);
setTokenSentToServer(false);
});
}
// Callback fired if Instance ID token is updated.
messaging.onTokenRefresh(function() {
messaging.getToken()
.then(function(refreshedToken) {
console.log('Token refreshed.');
// Indicate that the new Instance ID token has not yet been sent to the
// app server.
setTokenSentToServer(false);
// Send Instance ID token to app server.
sendTokenToServer(refreshedToken);
// ...
})
.catch(function(err) {
console.log('Unable to retrieve refreshed token ', err);
showToken('Unable to retrieve refreshed token ', err);
});
});
I have created one firebase notification Web application.When I login from web application then I generate FCM token and send it to my API server this is working properly.I have written onTokenRefresh method in my code but I am not able to test this code. Is there any way to test this code?
firebase.initializeApp(config);
messaging = firebase.messaging();
// Get Instance ID token. Initially, this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
messaging.requestPermission().then(function() {
return messaging.getToken();
}).then(function(currentToken) {
if (currentToken) {
sendTokenToServer(currentToken);
updateUIForPushEnabled(currentToken);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
// Show permission UI.
updateUIForPushPermissionRequired();
setTokenSentToServer(false);
}
})
.catch(function(err) {
console.log('An error occurred while retrieving token. ', err);
showToken('Error retrieving Instance ID token. ', err);
setTokenSentToServer(false);
});
}
// Callback fired if Instance ID token is updated.
messaging.onTokenRefresh(function() {
messaging.getToken()
.then(function(refreshedToken) {
console.log('Token refreshed.');
// Indicate that the new Instance ID token has not yet been sent to the
// app server.
setTokenSentToServer(false);
// Send Instance ID token to app server.
sendTokenToServer(refreshedToken);
// ...
})
.catch(function(err) {
console.log('Unable to retrieve refreshed token ', err);
showToken('Unable to retrieve refreshed token ', err);
});
});
Share
Improve this question
edited Feb 9, 2017 at 14:44
Frank van Puffelen
601k85 gold badges890 silver badges860 bronze badges
asked Feb 9, 2017 at 12:06
Rohit ShelhalkarRohit Shelhalkar
7661 gold badge9 silver badges15 bronze badges
1
- I send message after 1hr from my API server then I get following error MulticastResult(multicast_id=76342......7621,total=7,success=4,failure=3,canonical_ids=2,results: [[ messageId=0:148...........9fd7ecd canonicalRegistrationId=APA91b............Xa5c2v9 ], [ errorCode=NotRegistered ], [ errorCode=NotRegistered ], [ messageId=0:14......cd ], [ messageId=0:14....cd ], [ messageId=0:14....cd canonicalRegistrationId=APA9...........pUNB2QGu ], [ errorCode=NotRegistered ]] When I call from my dev console get new firebase notification token . – Rohit Shelhalkar Commented Feb 9, 2017 at 14:26
1 Answer
Reset to default 5At the time of writing onTokenRefresh is not used.
In the future the library will change so that tokens can be expired and refreshed by FCM and this callback will be used to signal the change.
The best advice I can give is to "fake this event" by writing a test where you call getToken(), unregister the firebase messaging service worker (This will make the token invalid by unsubscribing the the PushSubscription):
navigator.serviceWorker.getRegistrations().then((regs) => {
return Promise.all(regs.map(reg => reg.unregister()));
});
Then call the code you'd run in onTokenRefresh code manually.