Implementing a Firebase server-side countdown timer for Android?
Asked Answered
N

2

6

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?

Nonconformity answered 14/1, 2017 at 8:39 Comment(0)
S
0

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);
Succinic answered 14/1, 2017 at 10:30 Comment(0)
J
3

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

Firebase ref = new Firebase('/firebase/database/url/time');
    new AnimationTimer(){
        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(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot ds) {
            Platform.runLater(() -> {
            label.setText(ds.getValue() + "");
        });
        }

        @Override
        public void onCancelled(FirebaseError fe) {
            throw new UnsupportedOperationException("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

Julianjuliana answered 30/1, 2017 at 22:46 Comment(3)
This is my first answer on s.o. so please, let me know how I could improve my response.Julianjuliana
Would you give an example on what format would you pass dates inside start () {...} and stop (){...} ?Cariole
Do note, as firestore charges you on number of reads and writes. Doing this with firestore will be a very costly idea.Chemesh
S
0

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);
Succinic answered 14/1, 2017 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.