Your code suggests you want to get the current user's ID passed from Android native application back to Unity, which would be a little easier because Firebase caches the current user when you set up authentication in your app. Getting the current user id would look something like this in your Android app:
public String GetCurrentUserID()
{
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
To get the string out of that function, in Unity you would do something like:
AndroidJavaObject unityPlayerClass = new AndroidJavaObject(pluginName);
Debug.Log(unityPlayerClass.Call<string>("GetCurrentUserId"));
If your native Android app is handling your Firebase calls and you are trying to get more complex data out of your realtime database, you should set up a handler to get the results from your request in your native app, then send the results back to Unity from there.
The article you linked describes how to get your DataSnapshot from Firebase in Java, the only thing to add would be a call back to Unity with the value from your DataSnapshot, and a function to receive the value inside of Unity.
Your Java code in your native app could look something like:
public void GetUserName(String uid) {
databaseReference.child(String.format("users/%s/name", uid)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Send the snapshot value, when received, back to Unity
// "GameObject" should be the name of your gameObject in your active scene
UnityPlayer.UnitySendMessage("GameObject", "ReceiveUserName", dataSnapshot.getValue(String.class));
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
Then in Unity you could have a method ReceiveUserName
:
string userId;
void Start()
{
Text textCanvas = GameObject.Find("Canvas/Text").GetComponent<Text>();
RequestUserId();
}
// This doesn't return anything, it just sends the request out to the Android app
public void RequestUserId()
{
AndroidJavaObject unityPlayerClass = new AndroidJavaObject(pluginName);
unityPlayerClass.Call("GetUserId", userId);
}
// This gets called from the Android app, when the onDataChange listener is called.
public void ReceiveUserName(string name)
{
Debug.Log("The message is " + name);
}
Depending where you're at with your development, you could also look into using the Firebase Unity SDK, which allows you to make your Firebase calls from Unity directly.