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.
The java support for Firebase is intended to extends its server side functionality by using Firebase's sdk. To query on Firebase realtime database or Firestore, you should write your android/ios app and/o through a web app written on javascript.
Firebase REST APIs allow you to make requests to the Firebase Database for reading, writing, updating, or removing data. You can define any Firebase Realtime Database URL as a REST endpoint by adding . json in the end. Keeping your data safe, you can use the Firebase REST API to send requests via an HTTPS client.
The first step in creating an API in Firebase is to access the Firebase console and add a project by clicking “Add project” and giving the new project a name. Google will give you the option to enable Google Analytics for your new project.
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.
Follow the steps from https://firebase.google.com/docs/server/setup
Which can be broken down into:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With