I have a Flutter app that creates a FCM Token on the first run, like this:
_firebaseMessaging.getToken().then((token) {
//save my token here
});
However, I understand that this token can be refreshed once in awhile. In order to get this new refreshed token, I must call onTokenRefresh
method:
Stream<String> fcmStream = _firebaseMessaging.onTokenRefresh;
fcmStream.listen((token) {
saveToken(token);
});
The problem is that I don't know if this it is correct. The line saveToken(token)
is always execute when the app runs, but it works when the app is not on foreground/background?
I mean, this onTokenRefresh
will keep listening even if the user closes the app?
If not, how do I get the new token if the app doesn't get started for a long time?
yes it does not change token upon updating.
You need to call sendRegistrationToServer() method which will update token on server, if you are sending push notifications from server. UPDATE: New Firebase token is generated ( onTokenRefresh() is called) when: The app deletes Instance ID.
Base on this firebase document fcm token will changed on below events:
If the app doesn't get started for a long time, and none of the above events has occurred, the app token will not changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With