B4J Question ABMaterial Support for FireBase

walterf25

Expert
Licensed User
Longtime User
Hi, I noticed that ABMaterial supports FireBase functions, I have the need to write and read from a RealTimeDataBase, I noticed that some of the fields required are missing from the latest 4.3 version, any idea if the library can be updated to include some of the other required fields, I believe they have added some fields since the last FireBase Admin skd update.

At the moment on the following fields are exposed on ABMaterial version 4.3
page.Firebase.ApiKey = ""
page.Firebase.AuthDomain = ""
page.Firebase.DatabaseURL = ""
page.Firebase.StorageBucket = ""

While the new FireBase Admin SDK requires all the following fields with MeasurementId being Optional.
firebases.PNG


Is it too much work to update the Library to include projectId, messagingSenderId and AppId, i'm not even sure if all these are needed but it wouldn't hurt to have them available.

Thanks,
Walter
 

alwaysbusy

Expert
Licensed User
Longtime User
There is only a very limited wrap for Firebase included in ABM. Only Auth and Storage v3.0 are wrapped.

I suggest you skip the build-in methods and use the standard B4J calls to include more features or a higher version.

Example (untested):
B4X:
' in your class Initialize add
page.AddExtraJavaScriptFile("https://www.gstatic.com/firebasejs/7.22.1/firebase-app.js")
page.AddExtraJavaScriptFile("https://www.gstatic.com/firebasejs/7.22.1/firebase-database.js")
page.AddExtraJavaScriptFile("myfirebase.js") ' to hold our global javascript variables and some functions if needed

' in ConnectPage()
Dim script as String = $"
const firebaseConfig = {
  apiKey: "AIzaSyCGQ0tYppWFJkuSxBhOpkH0xVDmX245Vdc",
  authDomain: "project-id.firebaseapp.com",
  databaseURL: "https://project-id.firebaseio.com",
  projectId: "project-id",
  storageBucket: "project-id.appspot.com",
  messagingSenderId: "637908496727",
  appId: "2:637908496727:web:a4284b4c99e329d5",
  measurementId: "G-9VP01NDSXJ"
};
firebase.initializeApp(config);

database = firebase.database();
"$

Page.ws.Eval(script, null)
Page.ws.Flush

myfirebase.js
B4X:
var database;

function writeUserData(userId, name, email, imageUrl) {
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });
}

if you want some event in javascript to run a B4J method, use this:
B4X:
b4j_raiseEvent('page_parseevent', {'eventname': 'page_youreventlowercased','eventparams':'yourparam1, yourparam2',yourparam1': value1, 'yourparam2': value2});

In B4J you can then use:
B4X:
Sub Page_YourEventNameLowerCased(yourParam1 as String, yourParam2 as int)
  ' here you can do stuff and for example call one of the javascript methods:
dim script as String = $"
writeUserData(15, "Alain", "[email protected]", "http://domain.com/myimage.jpg");
"$
Page.ws.Eval(script, Null)
Page.ws.Flush
End Sub

Note on the event because of a B4X limitation that a CallSub can only have max 2 parameters: If you have more than 2, then you will need to define your Sub in B4J with a map as Parameter
B4X:
Sub Page_YourEventNameLowerCased(params as Map)
    Dim yourParam1 as String = params.Get("yourparam1") ' Lowercased!
    Dim yourParam2 as Int = params.Get("yourparam2") ' Lowercased!
    Dim yourParam3 as String = params.Get("yourparam3") ' Lowercased!
End Sub

Alwaysbusy
 
Upvote 0
Top