Is it ok to have the firebase FCM initialization not at startup but after authentication?

164

You can do it for sure. I have used FCM in one of my project where if user is logged in I save the token but if not I first ask the user to login and after that I use to save the device token. And this should be used only, otherwise the messages receive on the device even if the user is not logged in to the app, and it seems to be very weird.

Note: Remember to delete the token as well if the user is logged out from the application.

Share:
164
ale
Author by

ale

Updated on January 04, 2023

Comments

  • ale
    ale over 1 year

    In order to first-time register FCM

    _firebaseMessaging.getToken().then((String? token) {
      _setToken(token);
    });
    

    in the app, I need an internet connection to install the (FIS) firebase installation services and use firebase services like FCM. Currently, I have the above code in the init state of the root widget but don't check for an internet connection until later, this throws an error if the user runs the app without internet after a fresh installation. Is it safe to move the fcm registration after the user authenticates? The firebase doc states that: https://firebase.google.com/docs/cloud-messaging/manage-tokens

    As noted in our client setup guides, your app should retrieve this token at initial startup and save it to your app server alongside a timestamp.

    But if two users share the same device with different accounts they could receive a push notification that was intended for each other if we retrieve the fcm token before authentication. So I believe the docs are not strictly referring to the very app startup

  • ale
    ale about 2 years
    thanks, I think this is actually how we are supposed to manage the fcm token, just got confused with the firebase docs