Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 340
Me123 Avatar asked Oct 30 '22 14:10

Me123


1 Answers

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

like image 55
Mathew Cross Avatar answered Nov 15 '22 06:11

Mathew Cross