22

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Notheros
  • 2,137
  • 9
  • 23
  • 34

1 Answers1

19

Base on this firebase document fcm token will changed on below events:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data

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.

Milad Aghamohammadi
  • 1,866
  • 1
  • 16
  • 29