Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.)

like image 502
Rohan Taneja Avatar asked Oct 17 '20 09:10

Rohan Taneja


People also ask

How can I store FCM token in database?

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.

How long do FCM tokens last?

Ensuring registration token freshness To cover all cases, you should adopt a threshold for when you consider tokens stale; our recommendation is two months. Any token older than two months is likely to be an inactive device; an active device would have otherwise refreshed its token.

Can FCM token change?

Similarly to how GCM works, the FCM token can change due to token-rotation.

Can a user have multiple FCM tokens?

Yes, but that doesn't change unless: Instance ID is stable except when: *App deletes Instance ID. *App is restored on a new device. *User uninstalls/reinstall the app. *User clears app data.


2 Answers

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

like image 96
Frank van Puffelen Avatar answered Oct 09 '22 13:10

Frank van Puffelen


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.

like image 2
Nilesh K Avatar answered Oct 09 '22 13:10

Nilesh K