xamarin.androidgoogle-playgoogle-play-servicesgoogle-play-consolegoogle-play-games

Xamarin Android - How to sign in Google Play Services?


I am trying to add Google Play Services to my Xamarin Android app.

I am using Play Games Services v2 SDK and trying to follow this tutorial from the Official documentation.

The Java code for signing in would be that:

GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(getActivity());

gamesSignInClient.isAuthenticated().addOnCompleteListener(isAuthenticatedTask -> {
  boolean isAuthenticated =
    (isAuthenticatedTask.isSuccessful() &&
     isAuthenticatedTask.getResult().isAuthenticated());

  if (isAuthenticated) {
    // Continue with Play Games Services
  } else {
    // Disable your integration with Play Games Services or show a
    // login button to ask  players to sign-in. Clicking it should
    // call GamesSignInClient.signIn().
  }
});

How can I translate it to C#? Anyone can help me please?

This is my best attempt, but I am getting an exception on SetJniIdentityHashCode method not implemented.

using Android.Gms.Games;
using Android.Gms.Tasks;

// ...

PlayGamesSdk.Initialize(this);
IGamesSignInClient gamesSignInClient = PlayGames.GetGamesSignInClient(this);
gamesSignInClient.IsAuthenticated().AddOnCompleteListener(
    new OnCompleteListener()
);

// ...

public class OnCompleteListener : Java.Lang.Object, IOnCompleteListener
{
    public void Disposed()
    {
        throw new NotImplementedException();
    }

    public void DisposeUnlessReferenced()
    {
        throw new NotImplementedException();
    }

    public void Finalized()
    {
        throw new NotImplementedException();
    }

    public void OnComplete(Task task)
    {
        //var isAuthenticated =
        //      (task.IsSuccessful && 
        //      ((????)task.Result).isAuthenticated())

        //if (isAuthenticated)
        //{
        //    // Continue with Play Games Services
        //}
        //else
        //{
        //    // Disable your integration with Play Games Services or show a
        //    // login button to ask  players to sign-in. Clicking it should
        //    // call GamesSignInClient.signIn().
        //}
    }

    public void SetJniIdentityHashCode(int value)
    {
        throw new NotImplementedException();
    }

    public void SetJniManagedPeerState(JniManagedPeerStates value)
    {
        throw new NotImplementedException();
    }

    public void SetPeerReference(JniObjectReference reference)
    {
        throw new NotImplementedException();
    }
}

Solution

  • I managed to get the authentication result in the following way, probably was just using the wrong references.

    public class TaskCompleteListener : Java.Lang.Object, IOnCompleteListener
    {
        public void OnComplete(Android.Gms.Tasks.Task task)
        {
            var isAuthenticated = task.IsSuccessful &&
                ((AuthenticationResult)task.Result).IsAuthenticated;
    
            if (isAuthenticated)
            {
                // Continue with Play Games Services
            }
            else
            {
                // Disable your integration with Play Games Services or show a
                // login button to ask  players to sign-in. Clicking it should
                // call GamesSignInClient.signIn().
            }
        }
    }