2

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
sajidamin
  • 49
  • 1
  • 1
  • 10

2 Answers2

5

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. Then, if you want to check if the user is online, just check if his id exists in the list.

You can also add a new property named isOnline for each user in your database and then update it 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 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. 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");
        }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • and what can i do for the internet connection – sajidamin May 23 '18 at 06:46
  • You need to go online. You should find an internet spot in order to make changes in your Firebase database. – Alex Mamo May 23 '18 at 06:48
  • how can i do all this senioro can u please send me full source code – sajidamin May 23 '18 at 07:03
  • I don't understand you, a full source code for what? In my answer above, I have explained you how how you can get the state of the user. I recommend you also checking out the documentation and trying it. If you get stuck during a specific step, show what you've tried. – Alex Mamo May 23 '18 at 07:10
  • Is there everything alright? Have you solved the issue? – Alex Mamo May 24 '18 at 05:55
  • @AlexMamo thanks for this answer. I had totally missed that section of docs. Btw, in [this](https://firebase.google.com/docs/database/android/offline-capabilities#section-sample) they have mentioned how to attach a `ValueEventListener` on `.info/connected`. Can you tell me when to add this listener and when to remove this in my app. – Sourav Kannantha B May 15 '21 at 10:52
  • @SouravKannanthaB I think you might be interested in this [answer](https://stackoverflow.com/questions/48861350/should-i-actually-remove-the-valueeventlistener/48862873). – Alex Mamo May 17 '21 at 08:10
2

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

      }
    });
linker
  • 821
  • 1
  • 8
  • 20