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?