How to refresh firebase token on Flutter?
Asked Answered
W

5

9

I have a Flutter app that uses Firebase messaging to delivery notifications. This is the base code, it does nothing special, besides saving the token on my DB.

 FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();

 _firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) {

  },
  onResume: (Map<String, dynamic> message) {

  },
  onLaunch: (Map<String, dynamic> message) {

  },
);


_firebaseMessaging.getToken().then((token) {
  saveToken(token);
});

Do I have to implement some kind of background service to keep saving the new token on my DB everytime it gets refreshed? I remember using onTokenRefresh() on Android(JAVA) to do this, but I found nothing about it in Flutter (DART).

I read somewhere that the token gets refreshed every 3600 seconds. I wonder if this is true.

Whilom answered 24/7, 2018 at 17:36 Comment(0)
D
23

No, FCM token doesn't refresh every 3600 seconds. It only refreshes when :

  1. When user Uninstall/Reinstall the app or Clears App Data
  2. You manually delete FCM Instance using FirebaseMessaging().deleteInstanceID()

You can listen to token refresh stream using:

FirebaseMessaging().onTokenRefresh.listen((newToken) {
   // Save newToken
});

Hope it helps

Dryasdust answered 29/12, 2019 at 12:7 Comment(6)
i'm writing this onTokenRefresh inside initstate and its calling everytime not only on getting new token its called and return old token too. i need this onTokenRefresh only calls on new token generated only.Dead
Yes, as a workaround, you have to store the old token in sharedPreferences. Everytime you get a onTokenRefresh call, you can compare it with the one on SharedPreferences and store it only if they are different. Hope it helpsDryasdust
Thanks brother, i'm also thinking like that.Dead
@Dead Glad I could help.Dryasdust
possible that this is the correct syntax? FirebaseMessaging.instance.onTokenRefresh.listen((newToken) { // Save newToken });Biting
Just want to add that the method deleteInstanceID() is currently deprecated. Now you should call deleteToken() method instead.Iams
B
3

You can use firebaseMessaging.onTokenRefresh to get a stream which receives an event each time a new token is received.

Broad answered 24/7, 2018 at 19:19 Comment(0)
L
3

Here is an example of subscribing to the firebaseMessaging.onTokenRefresh stream and updating the token if the token has changed:

FirebaseMessaging().onTokenRefresh.listen((token) async {
  final prefs = await SharedPreferences.getInstance();
  final String firebaseTokenPrefKey = 'firebaseToken';
  final String currentToken = prefs.getString(firebaseTokenPrefKey);
  if (currentToken != token) {
    print('token refresh: ' + token);
    // add code here to do something with the updated token
    await prefs.setString(firebaseTokenPrefKey, token);
  }
});
Lection answered 29/10, 2020 at 12:21 Comment(1)
onTokenRefresh only gets called when the token changes. You don't need any logic there.Holmium
U
0
  1. You can try with this.. as per new updation
FirebaseMessaging.instance.onTokenRefresh.listen((newToken) {
        // Save newToken
      });
Unsex answered 8/12, 2022 at 9:35 Comment(0)
F
-5

After the user logs in my app logs her in again automatically every 3500 seconds.

I used a Timer like this:

  void _timerPressed() {
    const timeout = const Duration(seconds: 3500);
    new Timer.periodic(timeout, (Timer t) => _handleSignIn());
  }

I set the timer in the 'login' button press method after the login has occurred:

  void _loginPressed() {
    print('The user wants to login with $_email and $_password');
    _handleSignIn()
        .then((FirebaseUser user) => print(user))
        .catchError((e) => print(e));
    _timerPressed();
  }

(Don't be fooled by the name of the method, '_timerPressed'. I used a button press for testing the technique and haven't gotten around to renaming the method after I tied it in to the login button.)

Fresnel answered 29/12, 2019 at 9:41 Comment(1)
You don't even talk about FCM token in this post. FCM token is used for push notifications and is a totally different matter from authenticating user with firebase.Judah

© 2022 - 2024 — McMap. All rights reserved.