Search code examples
javaandroid-studiofirebasetimerserver-side

Implementing a Firebase server-side countdown timer for Android?


Is there a way to implement a Firebase server-side countdown timer in Android Studio?

I want the timer to be server side, meaning that whenever a user opens my app the counter will always be at the same time for all users.

I read the answers to this question, but they're 2+ years old and Firebase changed a lot since. It was acquired by Google in October 2014 and a significant number of new features were featured in May 2016 at Google I/O.

If Firebase can't provide that feature, is there another platform that can provide server-side countdown?


Solution

  • I dont have better idea for countdown only calculate on clientside.

    What happen if you do it:

    1. Get Server Time in timestamp (add to srvTime variable)

    Create new record for "countdown" timer:

    1. Start time: srvTime
    2. End time: Time in ms when countdown ended for example (5min = 60000*5)

    And you can calculate in client side.

    Or create a listener, for ServerValue.TIMESTAMP

    ref.addValueEventListener(new ValueEventListener() {
        public void onDataChange(DataSnapshot dataSnapshot) {
            System.out.println(dataSnapshot.getValue()); 
        }
    
        public void onCancelled(DatabaseError databaseError) { }
    });
    ref.setValue(ServerValue.TIMESTAMP);