Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Get current date and time from firebase

I am developing a simple game where users get daily bonus.I want to get current date and time from firebase server as i don't want to rely on device time which could be manipulated.
I went through ServerValue.TIMESTAMP in firebase but don't know how to use it.It returns a map with value as timestamp.How can i convert that timestamp into date.I want to get Today's date from firebase when ever user clicks on daily bonus.

like image 639
JakeGosemath Avatar asked Apr 16 '18 19:04

JakeGosemath


2 Answers

Since Firebase Introduced Callable Functions, you can easily use it in your app by creating a Callable Function in Firebase Cloud Functions. in your index.js, create a Function and make it return the current timestamp

exports.getTime = functions.https.onCall((data,context)=>{
return Date.now()
})

then deploy it to Firebase Cloud Functions

then in your Android App add the Callable Functions dependency

implementation 'com.google.firebase:firebase-functions:16.1.0'

then call the function from your app like this, and make sure you are typing the same name of the function 'getTime' as in your Cloud Function

    FirebaseFunctions.getInstance().getHttpsCallable("getTime")
            .call().addOnSuccessListener(new OnSuccessListener<HttpsCallableResult>() {
        @Override
        public void onSuccess(HttpsCallableResult httpsCallableResult) {
            long timestamp = (long) httpsCallableResult.getData();

        }
    });

you can also make a Simple interface if you want to call this method in multiple classes

public interface OnGetServerTime {
    void onSuccess(long timestamp);

    void onFailed();
}

 public void getServerTime(final OnGetServerTime onComplete) {
    FirebaseFunctions.getInstance().getHttpsCallable("getTime")
            .call()
            .addOnCompleteListener(new OnCompleteListener<HttpsCallableResult>() {
                @Override
                public void onComplete(@NonNull Task<HttpsCallableResult> task) {
                    if (task.isSuccessful()) {
                        long timestamp = (long) task.getResult().getData();
                        if (onComplete != null) {
                            onComplete.onSuccess(timestamp);
                        }
                    } else {
                        onComplete.onFailed();
                    }
                }
            });

}
like image 99
3llomi Avatar answered Nov 11 '22 10:11

3llomi


ServerValue.TIMESTAMP is just a token that Firebase Realtime Database converts to a number on the server when it's used as a child value during write operation. The date only appears in the database after the write completes.

If you want that value, you can read it back out of the database at the location you wrote it. But you should understand that this requires two round trips with the database, first to write it, then again to read it. By the time you get the value back, it will no longer be a perfect representation of the current moment in time, as it represents the moment in time that the value was initially written.

like image 33
Doug Stevenson Avatar answered Nov 11 '22 11:11

Doug Stevenson