sajidamin
sajidamin

Reputation: 49

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

Upvotes: 2

Views: 14530

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138834

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");
        }
});

Upvotes: 5

linker
linker

Reputation: 891

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

      }
    });

Upvotes: 2

Related Questions