7

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.

Martin
  • 474
  • 2
  • 6
  • 17
  • Possible duplicate of [Firebase SDK 9.0.0 plain java version?](http://stackoverflow.com/questions/37455308/firebase-sdk-9-0-0-plain-java-version) – eikooc May 29 '16 at 19:11
  • No, I checked that link and I cannot get what I want. Sorry I might sound lazy but am doing something urgent thats why am posting a question here. I was wondering if I could get a sample code of how to initialize and or use firebase with java with pointers of where to download the jars – Martin May 29 '16 at 19:18
  • You can download it from Maven. http://mvnrepository.com/artifact/com.google.firebase/firebase-server-sdk/3.0.0 – eikooc May 29 '16 at 19:25
  • And the getting started guide is here https://firebase.google.com/docs/server/setup#add_the_sdk – eikooc May 29 '16 at 19:26
  • Yea I have seen the getting started guide. Where do you get the "serviceAccountCredentials.json" in the sample code: FirebaseOptions options = new FirebaseOptions.Builder() .setServiceAccount(new FileInputStream("path/to/serviceAccountCredentials.json")) .setDatabaseUrl("https://databaseName.firebaseio.com/") .build(); FirebaseApp.initializeApp(options); – Martin May 30 '16 at 13:15
  • You download it from the firebase console as specified in this section https://firebase.google.com/docs/server/setup#add_firebase_to_your_app – eikooc May 30 '16 at 15:25
  • Thank you @eikooc You are really helpful. – Martin May 30 '16 at 18:12
  • Could you probably write a small steps instruction for getting started. I will mark it as the answer. – Martin May 30 '16 at 18:13

3 Answers3

7

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.

Jakob Hartman
  • 346
  • 3
  • 8
2

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.

eikooc
  • 2,363
  • 28
  • 37
1

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

Hai nguyen thanh
  • 718
  • 7
  • 17