Android System.currentTimeMillis() value is different on firebase database than generated on app logcat

Viewed 212

I am submitting a user last seen on Firebase Realtime Database and before setting value at firebase database I am generating a log with timestamp by System.currentTimeMillis() but when I compare it with logcat and database timestamp it submits a different timestamp on the server.

Look at the logcat it shows the correct timestamp and it should be on the server too

Disconnect with the firebase server 
Offline Time 5:01 pm 1639308715905

Firebase Realtime Database Value of Timestamp enter image description here

1639308009264

Both Values are different that's how I get wrong last seen of the user

1639308715905 - App one 
1639308009264 - Server One

The code I am using for log and to set a value on the server.

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "Main Activity";
   
    FirebaseDatabase DatabaseInstance;
    DatabaseReference infoConnected;
    



    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "On Resume Running");        
        DatabaseInstance.goOnline();
        
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "ON STOP CALLING");
        DatabaseInstance.goOffline();


    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DatabaseInstance = FirebaseDatabase.getInstance(); // Single Instance
        infoConnected = DatabaseInstance.getReference(".info/connected"); // To check firebase connection state
        initialiseOnlineStatus();

        setContentView(R.layout.main);
    }
        



    
    private void initialiseOnlineStatus() {
        final DatabaseReference Online = DatabaseInstance.getReference("Users/" + userID + "/Online");
        final DatabaseReference lastOnline = DatabaseInstance.getReference("Users/" + userID + "/lastSeen");


        infoConnected.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                boolean connected = dataSnapshot.getValue(Boolean.class);

                if (connected) {
                    Online.setValue(Boolean.TRUE);
                    lastOnline.removeValue();

                    Log.d(TAG, "Connected To Firebase Server ");
                } else {
                    Log.d(TAG, "Disconnect with firebase server ");
                    Online.onDisconnect().setValue(Boolean.FALSE);
                    String offlineTime = String.valueOf(System.currentTimeMillis());
                    Log.d(TAG, "Offline Time " + App_Functions.getLocalDeviceTimestamp(Long.parseLong(offlineTime)) + " " + offlineTime); // Get local device time from milliseconds
                    lastOnline.onDisconnect().setValue(offlineTime);
                    
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


    }



} 

Update: I noticed that the last timestamp (generated by an app) is uploading not the current one, I really don't understand how my last seen is not updating by the current timestamp. Please check gist too for the complete code

I also noticed that on the launch of my activity, the firebase function call twice, first with value boolean connected = dataSnapshot.getValue(Boolean. class); false than true. So with the false value, I am getting the first timestamp which should not update when activity onStop calls and when the app calls onStop it updates the timestamp which was first created on app launch.

3 Answers

I want to say thanks to @argzdev and @frank For helping me out at github. The solution i got from Github by @argzdev:

The reason this doesn't work is because you're trying to set a value after you've already disconnected the connection, it's bound to cause issues.

What you can do is change the logic of how you store the timestamp. Simply store the timestamp BEFORE you sever the connection. There shouldn't be any difference in time, even if there was, it would only be miniscule.

Relevant code:

 @Override
>     protected void onResume() {
>         super.onResume();
>         Log.d(TAG, "On Resume Running");
> 
>         Online.setValue(Boolean.TRUE);
>         lastOnline.removeValue();
>         DatabaseInstance.goOnline();
>     }
> 
> @Override
>     protected void onStop() {
>         super.onStop();
>         Log.d(TAG, "ON STOP CALLING");
> 
>         Online.setValue(Boolean.FALSE);
>         lastOnline.setValue("" + System.currentTimeMillis());
>         DatabaseInstance.goOffline();
>     }
> 

Then you can simply remove the onDisconnect in your ValueEventListener.

When you call this in your code:

lastOnline.onDisconnect().setValue(offlineTime);

This sends a literal value to the server, with the instruction to write that value when it detects that the client has disconnected. The server has no knowledge of the meaning of the value, and can't make any changes to it.

So if a different value is being written on the server than is being logged in the client, the only reason I can think of is that your calling onDisconnect (elsewhere?) too with a different value.

System.currentTimeMillis() will get you the epoch time of your emulator , meaning whatever the emulator or the phone system time it will give it to you

Related