Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating FCM Token on Flutter App

Tags:

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?

like image 351
Notheros Avatar asked Jul 25 '18 13:07

Notheros


People also ask

Does FCM token change on app update?

yes it does not change token upon updating.

How do I get new FCM tokens?

You need to call sendRegistrationToServer() method which will update token on server, if you are sending push notifications from server. UPDATE: New Firebase token is generated ( onTokenRefresh() is called) when: The app deletes Instance ID.


1 Answers

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.

like image 72
Milad Aghamohammadi Avatar answered Oct 16 '22 20:10

Milad Aghamohammadi