firebase connection check online offline status in android

随声附和 提交于 2019-12-24 08:35:26

问题


If user turn off both wi-fi, 3g, 4g, and so on and reverse (no internet connection). 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.

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

Kindly some one help me for this problem.. Thanks in advanced.


回答1:


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. A simple solution be to create a node where all users would inject their online status each. e.g.

    //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 i.e.

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

      }
    });



回答2:


To solve this, you can create a new node in your Firebase database to hold all online users, so when the user opens the application, you'll immediately add his id to this new created node. Then, if you want to check if the user is online, just check if his id is in the list.

You can also add a new property named isOnline for each user in your database and then accordingly.

For that, I recommend you using Firebase's built-in onDisconnect() method. It enables you to predefine an operation that will happen as soon as the client becomes disconnected.

See Firebase documentation.

You can also detecting 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. 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");
        }
});


来源:https://stackoverflow.com/questions/50480943/firebase-connection-check-online-offline-status-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!