Ayan Chakrabarty
Ayan Chakrabarty

Reputation: 21

Firebase, cant get instance ID token (Unity C#)

I need the instance ID token(or registration token) associated with my app to validate an A/B testing experiment on a test device . I have tried using the following code mentioned in the Firebase docs, but that hasnt helped me so far.

    Firebase.InstanceId.FirebaseInstanceId.DefaultInstance.GetTokenAsync().ContinueWith(task => {
if (!(task.IsCanceled || task.IsFaulted) && task.IsCompleted) {
  UnityEngine.Debug.Log(System.String.Format("Instance ID Token {0}", task.Result));
  }
});

Any help would be appreciated. Thanks

Upvotes: 2

Views: 4593

Answers (3)

kLScelxifa
kLScelxifa

Reputation: 26

Did you request a token? Taking Google Play as an example, the code should be written as follows:

public void GooglePlayGames_Initialize()
{
    PlayGamesPlatform.InitializeInstance(new PlayGamesClientConfiguration.Builder()
        .RequestIdToken()
        .RequestEmail()
        .Build());
    PlayGamesPlatform.DebugLogEnabled = true;
    PlayGamesPlatform.Activate();
    PlayGamesPlatform.Instance.Authenticate(Authentication);
}

internal void Authentication(bool status)
{
    if (status)
    {
        Firebase_GetUID();
    }
    else
    {
        // Sign in Failed!
    }
}

public void Firebase_GetUID()
{
    Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;

    auth.SignInWithCredentialAsync(Firebase.Auth.GoogleAuthProvider.GetCredential(PlayGamesPlatform.Instance.GetIdToken(), null)).ContinueWith(task =>
    {
        if (task.IsCanceled)
        {
            // SignInWithCredentialAsync was canceled
            return;
        }
        if (task.IsFaulted)
        {
            // SignInWithCredentialAsync is Faulted
            return;
        }

        isInitialize = true;
        uid = task.Result.UserId;
    });
}

Upvotes: 0

Ando
Ando

Reputation: 1837

Newer Firebase versions for Unity use the following

string installationId = await FirebaseInstallations.DefaultInstance.GetIdAsync(); // Most likely this is your case
string installationId = await FirebaseInstallations.GetInstance(app).GetIdAsync(); // Option 2 with particular app
Debug.Log(" FirebaseID is " + installationId);

Upvotes: 0

rene_buehling
rene_buehling

Reputation: 724

Using a Coroutine did the job for me:

private IEnumerator Start () 
{
    System.Threading.Tasks.Task<string> t = Firebase.InstanceId.FirebaseInstanceId.DefaultInstance.GetTokenAsync();
    while (!t.IsCompleted) yield return new WaitForEndOfFrame();
    Debug.Log(" FirebaseID is " + t.Result);

}

Also note that...

IsCompleted will return true when the task is in one of the three final states: RanToCompletion, Faulted, or Canceled.

according to MS docs. So no need to test all three states.

Upvotes: 1

Related Questions