티스토리 뷰

728x90
반응형
  1. Nuget 설치

  2. Android option > Android Pakage 서명 > APK서명

  3. google-services.json 다운로드 > 속성 > GoogleServicesJson

  4. Firebase console hashcode Sha1, Sha256 중요!!
    참고 서명만들기: keytool -exportcert -alias 별명 -keystore 경로.keystore -list -v
    서명을 만들면 Sha1, Sha256 hashcode를 얻을수 있고. Firebase Console에 설정한다.
    hashcode가 없으면... Firebase에 인증이 안되고. reCapChar에러가 생기고 문자 메시지를 전송할 수 없다.

AndroidManifest.xml

  1. Adroid Service folder Add
  2. AccountService.cs add

using System;
using System.Threading.Tasks;
using Android.Gms.SafetyNet;
using #####.Models.FirebasePageModel;
using #####.Services.Account;
using Firebase;
using Firebase.Auth;
using Firebase.Firestore;
using Java.Util.Concurrent;
using Xamarin.Essentials;
using Xamarin.Forms;

[assembly: Dependency(typeof(#####.Droid.Services.AccountService))]
namespace #####.Droid.Services
{
/// IAccountService 인터페이스를 서비스 클래스에 만든다.
// public interface IAccountService
// {

// Task SendOtpCodeAsync(string phoneNumber);

// Task VerifyOtpCodeAsync(string code);

// }

  public class AccountService : PhoneAuthProvider.OnVerificationStateChangedCallbacks, IAccountService
  {
          private PhoneAuthCredential _credential;
      public override void OnVerificationCompleted(PhoneAuthCredential credential)
      {

          System.Diagnostics.Debug.WriteLine("################# PhoneAuthCredential created Automatically");
          _credential = credential;
      }
        private PhoneAuthProvider.ForceResendingToken _forceResendingToken;
      public override void OnVerificationFailed(FirebaseException exception)
      {
          System.Diagnostics.Debug.WriteLine("Verification Failed:" + exception.Message);
          _phoneAuthTcs?.TrySetResult(false);
      }
        public override void OnCodeSent(string verificationId, PhoneAuthProvider.ForceResendingToken forceResendingToken)
      {
          base.OnCodeSent(verificationId, forceResendingToken);
          _verificationId = verificationId;




          this._forceResendingToken = forceResendingToken;
          _phoneAuthTcs?.TrySetResult(true);


      }


  private TaskCompletionSource<bool> _phoneAuthTcs;

public Task<bool> SendOtpCodeAsync(string phoneNumber)
      {
          if (string.IsNullOrWhiteSpace(phoneNumber))
          {
              return Task.FromResult(false);
          }



          try
          {
              _phoneAuthTcs = new TaskCompletionSource<bool>();


              FirebaseAuth auth = FirebaseAuth.Instance;

              PhoneAuthOptions options = PhoneAuthOptions.NewBuilder(auth)
              .SetPhoneNumber(phoneNumber)       // Phone number to verify
              .SetTimeout((Java.Lang.Long)120L, TimeUnit.Seconds) // Timeout and unit
              .SetActivity(Platform.CurrentActivity)                 // Activity (for callback binding)
              .SetCallbacks(this)          // OnVerificationStateChangedCallbacks
              .Build();

              PhoneAuthProvider.VerifyPhoneNumber(options);

  return Task.FromResult(true);// _phoneAuthTcs.Task; //Task.FromResult(true);//



          }
          catch (Exception Ex)
          {
              Console.WriteLine("Firebase Phoneauth Exception:" + Ex.Message + "//////");
              return  null;
          }




      }

 private string _verificationId;


      private void OnAuthCompleted(Task task, TaskCompletionSource<bool> tcs)
      {
          if (task.IsCanceled || task.IsFaulted)
          {
              tcs.SetResult(false);
              return;
          }
          _verificationId = null;
          tcs.SetResult(true);
      }
      public Task<bool> VerifyOtpCodeAsync(string code)
      {


          if (!string.IsNullOrWhiteSpace(_verificationId))
          {

              var tcs = new TaskCompletionSource<bool>();

              var credential = PhoneAuthProvider.GetCredential(_verificationId, code);


              FirebaseAuth.Instance.SignInWithCredentialAsync(credential)
                  .ContinueWith((task) => OnAuthCompleted(task, tcs));
              Console.WriteLine("__________________________///:" + tcs.Task.ToString());
              return  tcs.Task;

          }
          else
          {
              Console.WriteLine("__________________________///:Else");
              return null;
          }
  }
반응형
댓글