0

So I am successfully able to pass normal strings from Android to Unity which is integrated into my native app. But how do I pass a String that is fetched from Firebase? I know that Firebase is asynchronous and the value is not returned in a method which contains a value event listener.

I found this Stack Overflow thread:

How to return DataSnapshot value as a result of a method?

But that involves many calls. How can I handle that in Unity to get that String?

My Unity code is simple:

void Start()
    {

        Text textCanvas = GameObject.Find("Canvas/Text").GetComponent<Text>();


        string user = AndroidOperations.ReturnString();


        Debug.Log("The message is "+ user);

    }


public static string ReturnString() {
        AndroidJavaObject unityPlayerClass = new AndroidJavaObject(pluginName);

        return unityPlayerClass.Call<string>("getUserId");
    }

As you can see above I am calling the function "getUserId" present in my native Android code which contains a value event listener that is supposed to return a string fetched from Firebase. So is there any way I can do this? If so, what changes I should make in my Unity code?

jkdev
  • 11,360
  • 15
  • 54
  • 77

1 Answers1

0

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.

NSJacob1
  • 509
  • 3
  • 12
  • Thanks for the answer bro.Can I know what is the "GameObject" parameter in the UnitySendMessage?Should i just put as you said or i should put any gameobject name there? Yes i thought of using firebase for unity but since all my data comes from native app i thought creating another separate firebase project for unity would become redundant. I'll try the above method and let you know ! – Suhail Pappu Dec 23 '19 at 08:18
  • Bro I followed your steps but I am not getting the debug message of the method receiveUserId anywhere in my Log Cat.What should i do now?Also Above you specified the return type for getUserId as String but you didnt return anything? – Suhail Pappu Dec 23 '19 at 09:04
  • I added a little bit more in case you had something different in mind, and fixed up my code a little. Sorry about the errors, I don't often use Java any more. – NSJacob1 Dec 23 '19 at 19:52