Hi All I am Working in Firebase Sms. I have added the below code mentioned in the Firebase unity Phone Authentication. Code (CSharp): PhoneAuthProvider provider = PhoneAuthProvider.GetInstance(firebaseAuth); provider.VerifyPhoneNumber(phoneNumber, phoneAuthTimeoutMs, null, verificationCompleted: (credential) => { // Auto-sms-retrieval or instant validation has succeeded (Android only). // There is no need to input the verification code. // `credential` can be used instead of calling GetCredential(). }, verificationFailed: (error) => { // The verification code was not sent. // `error` contains a human readable explanation of the problem. }, codeSent: (id, token) => { // Verification code was successfully sent via SMS. // `id` contains the verification id that will need to passed in with // the code from the user when calling GetCredential(). // `token` can be used if the user requests the code be sent again, to // tie the two requests together. }, codeAutoRetrievalTimeout: (id) => { // Called when the auto-sms-retrieval has timed out, based on the given // timeout parameter. // `id` contains the verification id of the request that timed out. }); I have enabled phone Authentication,Sha1 fingerprint, in the Firebase Console.I have run in the above code in mobile only. Neither Code Sent is not Triggered and Error also not triggered. Could any one can help me. How can i send Phone Authentication sms from firebase using Unity. THE DOCUMENT IS NOT CLEAR....................... ABOUT THIS... Thanks in Advance...
Many people are think that above code is enough for sending mail from unity. It is core code. But apart from that we have to add many other code. I am posting my project code. Many game objects are unwanted. so kindly remove it or assign it. I have edited quick start examples of unity. Code (CSharp): using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using UnityEngine.UI; public class FirebaseSms : MonoBehaviour { protected Firebase.Auth.FirebaseAuth auth; protected Firebase.Auth.FirebaseAuth otherAuth; protected Dictionary<string, Firebase.Auth.FirebaseUser> userByAuth = new Dictionary<string, Firebase.Auth.FirebaseUser>(); protected bool signInAndFetchProfile = false; private uint phoneAuthTimeoutMs = 60 * 1000; private string phoneAuthVerificationId; public Button Button3; public InputField inp1,inp2; public GameObject inGameToggle; public GameObject SpriteReplaced; public Sprite Sprite1; public SpriteRenderer spriteR; public GameObject GoldOn, Goldoff; public GameObject AuthorizationCode; public GameObject EmailActivation,PhoneVerificationOff; public Image img1; public GameObject VerifiedSymbo1,VerifiedTick; Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther; private TouchScreenKeyboard keyboard; public Toggle musicToggle; public Material GreyScaleOn; public bool mobilecodecansent = false; public virtual void Start() { keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.NumbersAndPunctuation); Screen.orientation = ScreenOrientation.Portrait; // spriteR = SpriteReplaced.GetComponent<SpriteRenderer>(); Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => { dependencyStatus = task.Result; if (dependencyStatus == Firebase.DependencyStatus.Available) { InitializeFirebase(); } else { Debug.LogError( "Could not resolve all Firebase dependencies: " + dependencyStatus); } }); } public void SetMusic() { if (musicToggle.isOn) { GoldOn.GetComponent<Image>().material = null; mobilecodecansent = true; Debug.Log("I am ON"); } else { GoldOn.GetComponent<Image>().material = GreyScaleOn; mobilecodecansent = false; Debug.Log("I am Off"); // Set music Off } } protected void InitializeFirebase() { auth = Firebase.Auth.FirebaseAuth.DefaultInstance; img1.fillAmount = 0; } protected bool LogTaskCompletion(Task task, string operation) { bool complete = false; if (task.IsCanceled) { // Button3.GetComponentInChildren<Text>().text = "Cancelled"; } else if (task.IsFaulted) { // Button3.GetComponentInChildren<Text>().text = "Error"; foreach (Exception exception in task.Exception.Flatten().InnerExceptions) { string authErrorCode = ""; Firebase.FirebaseException firebaseEx = exception as Firebase.FirebaseException; if (firebaseEx != null) { // Button3.GetComponentInChildren<Text>().text = "AuthError"; authErrorCode = String.Format("AuthError.{0}: ", ((Firebase.Auth.AuthError)firebaseEx.ErrorCode).ToString()); } } } else if (task.IsCompleted) { VerifiedTick.SetActive(true); //PhoneVerificationOff.SetActive(false); //EmailActivation.SetActive(true); img1.fillAmount = 0.5f; Button3.GetComponentInChildren<Text>().text = "Verified"; Invoke("EmailActivation1", 4f); complete = true; } return complete; } public void EmailActivation1() { PhoneVerificationOff.SetActive(false); EmailActivation.SetActive(true); img1.fillAmount = 0.418f; } // Called when a sign-in without fetching profile data completes. void HandleSignInWithUser(Task<Firebase.Auth.FirebaseUser> task) { if (LogTaskCompletion(task, "Sign-in")) { } } // Called when a sign-in with profile data completes. void HandleSignInWithSignInResult(Task<Firebase.Auth.SignInResult> task) { if (LogTaskCompletion(task, "Sign-in")) { } } // Sign out the current user. protected void SignOut() { auth.SignOut(); } public void VerifyPhoneNumber2() { if (mobilecodecansent == true) { string k2 = inp1.text.ToString(); string k = "+91" + k2; Debug.Log("The Verify No is " + k); var phoneAuthProvider = Firebase.Auth.PhoneAuthProvider.GetInstance(auth); phoneAuthProvider.VerifyPhoneNumber(k, phoneAuthTimeoutMs, null, verificationCompleted: (cred) => { // Button3.GetComponentInChildren<Text>().text = "verificationcompleted"; if (signInAndFetchProfile) { auth.SignInAndRetrieveDataWithCredentialAsync(cred).ContinueWith( HandleSignInWithSignInResult); } else { auth.SignInWithCredentialAsync(cred).ContinueWith(HandleSignInWithUser); } }, verificationFailed: (error) => { // Button3.GetComponentInChildren<Text>().text = "error"; }, codeSent: (id, token) => { phoneAuthVerificationId = id; // Button3.GetComponentInChildren<Text>().text = "CodeSent"; Invoke("Time", 3f); }, codeAutoRetrievalTimeOut: (id) => { // Button3.GetComponentInChildren<Text>().text = "Timeout"; }); } } public void Time() { GoldOn.SetActive(false); AuthorizationCode.SetActive(true); VerifiedSymbo1.SetActive(true); GoldOn.SetActive(false); } public void VerifyReceivedPhoneCode2() { var phoneAuthProvider = Firebase.Auth.PhoneAuthProvider.GetInstance(auth); // receivedCode should have been input by the user. string k = inp2.text.ToString(); Debug.Log("The Code for verification is" + k); var cred = phoneAuthProvider.GetCredential(phoneAuthVerificationId, k); if (signInAndFetchProfile) { auth.SignInAndRetrieveDataWithCredentialAsync(cred).ContinueWith( HandleSignInWithSignInResult); } else { auth.SignInWithCredentialAsync(cred).ContinueWith(HandleSignInWithUser); } } }
The phone authentication will work only on the mobile platform. so you need to build the app and run on your phone and then enter the mobile number, then only you will receive the otp.
@Alexander21 I have a question regarding your code as I am fairly new to all this. In the VerifyPhoneNumber2 method, I see you are having Firebase verify the phone. From there, there are checks to see if the verification was completed, failed, code sent etc. My question is: When is the sms sent to the phone number that was provided? Sorry for asking as I am sure it is obvious but not seeing it.