Home > Net >  Database connection re-established on FCM notification on android
Database connection re-established on FCM notification on android

Time:06-24

In my Flutter/Firebase App I am using the Firebase Database feature .info/connected to indicate whether a device is online or not. So when a user opens the app, an entry it will be added to the list of active devices in .info/connected and vice versa. Then a cloud function is triggered which updates the user's Firestore isOnline record correspondingly.

Additionally I am using Firebase Cloud Messaging to send notifications to the users.

Here comes the problem: If a FCM notification is sent to an android device, where the app is currently in background and not connected to .info/connected, this connection gets re-established and the user is set to online even if the app was not opened.

This produces a false indication of the user status. Can I turn off this behavior?

CodePudding user response:

It sounds like receiving the FCM message reactivates the application code, which then reconnects to the database and (correctly) sets the .info/connected.

If you don't want to use this as a signal that the user is online (although clearly their app is online), you have a few options:

  • Pick a different action to signal that the user is active. For example use the app/screen lifecycle of your app, since you indicate that you want the opening of the app to be the event that signals the user being online.

    If you've already wired up onDisconnect, you might want to continue to use that for marking the user as offline in case of dirty disconnnects (like when the app crashes).

  • Explicitly manage the connection to the Firebase backend, by calling goOffline and goOnline in response to app/screen lifecycle events.

  • Related