FCM Token - When should I store/save it on my DB?

3,578

Solution 1

The FCM token is refreshed under conditions that you don't control, and those conditions have even changed over time. To handle token updates properly, you'll need to implement both initially getting the token and then monitoring for token updates.

Note that FCM tokens are not associated with a user. It is fine if you want to associate them with a user, but it's up to your application code in that case to maintain the association. So that for example includes deleting the token from your database when the user signs out, as you're doing in step 3. 👍

For keeping your token registry clean, you can indeed do this proactively as you intend, or reactively as shown here: https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js#L76-L88

Solution 2

Hi Rohan fundamentaly you should use below logic to save tokens on server.

Step1: as soon as you get token in callback whether new or same try to save it localstorage.

Step2: Call your REST API to save it to your server. it is upto you if you want to send unique user identifier along with the token.

Step3: It is obvious you will recieve token callback a lot of time so you can check whether you have similar token in localstorage, it means you have the token on the server so no point calling REST API.

Step 4: Now your app can send events back to server and based on it trigger Push notifications to the users.

Step 5: You can Add/update user token based on uniqye user identifier. In some cases a user can be guest user, so your app should generate guest userId and link it with token.

Stay safe.

Share:
3,578
Rohan Taneja
Author by

Rohan Taneja

Flutter Evangelist

Updated on December 25, 2022

Comments

  • Rohan Taneja
    Rohan Taneja over 1 year

    I am not sure what a proper FCM token handling mechanism would be so I’m writing our process down here just to get some validation or suggestions for improvements:

    1. Fetch FCM token on client Login (Flutter)
    2. Save FCM token on our Database (Using our REST API)
    3. Delete FCM token on Logout (Using our REST API)

    Q1: Should we be getting the FCM token more often than just on login? AFAIK, FCM token only changes on app re-installs, clearing cache, etc. Does this also include app-updates from the PlayStore? In that case, should we save the FCM token on every app launch since the user will remain logged in after an app update and hence we wouldn't trigger the save FCM call.

    Q2: Did I mention the right way to handle deleting FCM tokens from our DB? We don’t want the user to keep getting notifications once they have logged out.

    Q3: An add-on idea is to send the device_id to the server along with the fcm_token so that server deletes all previously saved FCM tokens for that device_id. This is useful to not have useless tokens on the DB from cases where the user uninstalls the app without logging out (which means that the DELETE fcm_token call never went through.)