Updating FCM Token on Flutter App

9,062

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.

Share:
9,062
Notheros
Author by

Notheros

Updated on December 06, 2022

Comments

  • Notheros
    Notheros over 1 year

    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?

  • Notheros
    Notheros over 5 years
    Thank you for your answer. If the documentation says that, then I have nothing to worry about.