Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh firebase token on Flutter?

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.

like image 495
Notheros Avatar asked Dec 13 '22 15:12

Notheros


2 Answers

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

like image 142
Susheel Karam Avatar answered Dec 31 '22 13:12

Susheel Karam


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

like image 29
rmtmckenzie Avatar answered Dec 31 '22 13:12

rmtmckenzie