Detect if Firebase connection is lost/regained

前端 未结 6 864
[愿得一人]
[愿得一人] 2020-11-27 12:25

Is there a strategy that would work within the current Firebase offering to detect if the server connection is lost and/or regained?

I\'m considering some offline co

6条回答
  •  春和景丽
    2020-11-27 12:35

    For android you can make user offline by just a single function called onDisconnect()

    I did this in one of my chat app where user needs to get offline automatically if network connection lost or user disconnected from Internet

    DatabaseReference presenceRef = FirebaseDatabase.getInstance().getReference("USERS/24/online_status");
    
    presenceRef.onDisconnect().setValue(0);
    

    On disconnecting from network Here I am making online_status 0 of user whose Id is 24 in firebase.

    getReference("USERS/24/online_status") is the path to the value you need to update on offline/online.

    You can read about it in offline capabilities

    Note that firebase takes time around 2-10 minutes to execute onDisconnect() function.

提交回复
热议问题