简体   繁体   English

Firebase 连接检查 Android 中的在线离线状态

[英]Firebase connection check online offline status in Android

If user turn off both wi-fi, 3g, 4g, and so on and reverse (no internet connection).如果用户同时关闭 wi-fi、3g、4g 等并反向(没有互联网连接)。 Firebase database name child connections:(true/false) So, when internet connections, wi-fi, 3g, 4g, and so on are off or missing, the user is offline so he can't be found. Firebase 数据库名称子连接:(true/false) 因此,当互联网连接、wi-fi、3g、4g 等关闭或丢失时,用户处于离线状态,因此无法找到他。

Remember the two scenarios: Before and After.记住两个场景:之前和之后。 If user is offline before an other user search him, then he will not displayed in the list result, if user is off-line after an other user search him, then it will display NO MORE AVAILABLE icon on the user如果用户在其他用户搜索他之前离线,那么他不会显示在列表结果中,如果用户在其他用户搜索他之后离线,那么它会在用户上显示 NO MORE AVAILABLE 图标

Kindly some one help me for this problem.请有人帮助我解决这个问题。

To solve this, you can create a new node in your Firebase Realtime Database to hold all online users, so when the user opens the application, you'll immediately add his id to this newly created node.为了解决这个问题,您可以在 Firebase 实时数据库中创建一个新节点来保存所有在线用户,这样当用户打开应用程序时,您将立即将他的 id 添加到这个新创建的节点中。 Then, if you want to check if the user is online, just check if his id exists in the list.然后,如果要检查用户是否在线,只需检查列表中是否存在他的id。

You can also add a new property named isOnline for each user in your database and then update it accordingly.您还可以为数据库中的每个用户添加一个名为isOnline的新属性,然后相应地更新它。

For that, I recommend you using Firebase's built-in onDisconnect() method.为此,我建议您使用 Firebase 的内置onDisconnect()方法。 It enables you to predefine an operation that will happen as soon as the client becomes disconnected.它使您能够预定义在客户端断开连接时将立即发生的操作。

See Firebase documentation .请参阅Firebase 文档

You can also detect the connection state of the user.您还可以检测用户的连接状态。 For many presence-related features, it is useful for your app to know when it is online or offline.对于许多与在线状态相关的功能,了解您的应用何时在线或离线非常有用。 Firebase Realtime Database provides a special location at /.info/connected which is updated every time the Firebase Realtime Database client's connection state changes. Firebase 实时数据库在/.info/connected提供了一个特殊位置,每次 Firebase 实时数据库客户端的连接状态更改时都会更新该位置。 Here is an example also from the official documentation:以下是官方文档中的一个示例:

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
    connectedRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            boolean connected = snapshot.getValue(Boolean.class);
            if (connected) {
                System.out.println("connected");
            } else {
                System.out.println("not connected");
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            System.err.println("Listener was cancelled");
        }
});

Though this is more than a year late, to clear up the confusion.虽然这已经晚了一年多,但还是要澄清一下混乱。 Alex's question would like to implement a live chat scenario in which each user can view everyone's online status at their ends or on their devices. Alex 的问题想要实现一个实时聊天场景,在该场景中,每个用户都可以在自己的终端或设备上查看每个人的在线状态。 A simple solution be to create a node where all users would inject their online status each.一个简单的解决方案是创建一个节点,所有用户都会在其中注入他们的在线状态。 eg例如

    //say your realtime database has the child `online_statuses`
    DatabaseReference online_status_all_users = FirebaseDatabase.getInstance().getReference().child("online_statuses");

    //on each user's device when connected they should indicate e.g. `linker` should tell everyone he's snooping around
    online_status_all_users.child("@linker").setValue("online");
    //also when he's not doing any snooping or if snooping goes bad he should also tell
    online_status_all_users.child("@linker").onDisconnect().setValue("offline")

So if another user, say mario checks for linker from his end he can be sure some snooping around is still ongoing if linker is online ie因此,如果另一个用户,比如mario从他的一端检查linker ,他可以确定如果linker在线,一些窥探仍在进行中,即

    DatabaseReference online_status_all_users = FirebaseDatabase.getInstance().getReference().child("online_statuses");
    online_status_all_users.child("@linker").addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
        String snooping_status = dataSnapshot.getValue(String.class);
        //mario should decide what to do with linker's snooping status here e.g.
         if(snooping_status.contentEquals("online")){
            //tell linker to stop doing sh*t
         }else{
            //tell linker to do a lot of sh****t
         }     
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM