Skip to content Skip to sidebar Skip to footer

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

Solution 1:

Create a server side timer that updates a value in your realtime database.

Firebase ref = newFirebase('/firebase/database/url/time');
    newAnimationTimer(){
        start(){...}
        stop(){...}
        handle(){...ref.addValue(time);}
        };

That will update your real-time database every millisecond. To see it in your application just call it.

Firebase ref = new Firebase('/firebase/database/url/time/value')

        ref.addValueEventListener(newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot ds) {
            Platform.runLater(() -> {
            label.setText(ds.getValue() + "");
        });
        }

        @OverridepublicvoidonCancelled(FirebaseError fe) {
            thrownewUnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

    });

It will update in real-time on your application in milliseconds. I was able to get this running in Java

Solution 2:

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() {
    publicvoidonDataChange(DataSnapshot dataSnapshot) {
        System.out.println(dataSnapshot.getValue()); 
    }

    publicvoidonCancelled(DatabaseError databaseError) { }
});
ref.setValue(ServerValue.TIMESTAMP);

Post a Comment for "Implementing A Firebase Server-side Countdown Timer For Android?"