How to use firebase with javafx

2020-06-17 06:10发布

am building a desktop application that I want to use the intuitive firebase API for live data syncs. I have been searching the web but no one points out what Jars to use and how to configure with your firebase application. If you can assist please give me steps. I am good at following steps. I want to create a helper class for all firebase operations.

3条回答
神经病院院长
2楼-- · 2020-06-17 06:15

It is risky to use the admin SDK on a client machine because it has admin rights to your database. I have developed a work around by creating a WebEngine that will read a html file that has no body just the firebase web scripts.

<!DOCTYPE html>
<html>`
<head>
    <script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>

    <title></title>
</head>
<body>
</body>
<script>
    try {
        var config = {
          //firebase config here
        };

        firebase.initializeApp(config);
        var data = firebase.database();
        var ref = data.ref("/ref");
        ref.on("value", function (snapshot) {
            //pass snapshot to java
            alert(JSON.stringify(snapshot.val()));
        });
    } catch (error) {
        alert("Error - jse:" + error.message);
    }
</script>
</html>

and to retrieve it in java

    //Start new webengine to run javascript.
    final WebEngine engine = new WebEngine();
    //get my html file.
    final URL url = NoficationTest.class.getResource("/javafx.html");
    //set to catch alerts from the javascript.
    engine.setOnAlert(event -> {
        try {

            final String data = event.getData();
            if (data != null) {
                if (data.contains("jse")) {
                      //handle if error in javascript.
                } else {
                      //handle if successfull
                }
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    });
    //Load html into webengine.
    engine.load(url.toString());

There is a better way for the javascript to call the java side of things, I just did it that way to have a fast answer. I have tested this and it works.

查看更多
萌系小妹纸
3楼-- · 2020-06-17 06:22

You can use the REST Api for accessing database as well as authentication. Check out this answer: https://stackoverflow.com/a/37419212/5287436

查看更多
手持菜刀,她持情操
4楼-- · 2020-06-17 06:35

Follow the steps from https://firebase.google.com/docs/server/setup

Which can be broken down into:

  • Create a firebase project
  • Download the SDK from Maven
  • Download the service account file which you can create from the web interface
  • Add the SDK
  • Initialize the SDK

I think the problem is that it is easy to overlook some of the steps on the website. Just follow them step by step and read carefully what steps are listed.

查看更多
登录 后发表回答