Search Unity

Firebase Sms is Not working

Discussion in 'Scripting' started by Alexander21, Aug 1, 2018.

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    Hi All

    I am Working in Firebase Sms. I have added the below code mentioned in the Firebase unity Phone Authentication.

    Code (CSharp):
    1. PhoneAuthProvider provider = PhoneAuthProvider.GetInstance(firebaseAuth);
    2. provider.VerifyPhoneNumber(phoneNumber, phoneAuthTimeoutMs, null,
    3.   verificationCompleted: (credential) => {
    4.     // Auto-sms-retrieval or instant validation has succeeded (Android only).
    5.     // There is no need to input the verification code.
    6.     // `credential` can be used instead of calling GetCredential().
    7.   },
    8.   verificationFailed: (error) => {
    9.     // The verification code was not sent.
    10.     // `error` contains a human readable explanation of the problem.
    11.   },
    12.   codeSent: (id, token) => {
    13.     // Verification code was successfully sent via SMS.
    14.     // `id` contains the verification id that will need to passed in with
    15.     // the code from the user when calling GetCredential().
    16.     // `token` can be used if the user requests the code be sent again, to
    17.     // tie the two requests together.
    18.   },
    19.   codeAutoRetrievalTimeout: (id) => {
    20.     // Called when the auto-sms-retrieval has timed out, based on the given
    21.     // timeout parameter.
    22.     // `id` contains the verification id of the request that timed out.
    23.   });



    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...
     
  2. anthonype927

    anthonype927

    Joined:
    Dec 6, 2013
    Posts:
    4
    Did you ever find a solution to this? I have the same issue.
     
  3. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    302
    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):
    1.  
    2.  
    3.  
    4.  
    5.  
    6. using System;
    7. using System.Collections;
    8. using System.Collections.Generic;
    9. using System.Threading.Tasks;
    10. using UnityEngine;
    11. using UnityEngine.UI;
    12.  
    13.  
    14.  
    15.  
    16.  
    17. public class FirebaseSms : MonoBehaviour
    18. {
    19.  
    20.     protected Firebase.Auth.FirebaseAuth auth;
    21.     protected Firebase.Auth.FirebaseAuth otherAuth;
    22.     protected Dictionary<string, Firebase.Auth.FirebaseUser> userByAuth =
    23.     new Dictionary<string, Firebase.Auth.FirebaseUser>();
    24.  
    25.     protected bool signInAndFetchProfile = false;
    26.  
    27.     private uint phoneAuthTimeoutMs = 60 * 1000;
    28.  
    29.     private string phoneAuthVerificationId;
    30.  
    31.     public Button Button3;
    32.     public InputField inp1,inp2;
    33.     public GameObject inGameToggle;
    34.     public GameObject SpriteReplaced;
    35.     public Sprite Sprite1;
    36.     public SpriteRenderer spriteR;
    37.     public GameObject GoldOn, Goldoff;
    38.     public GameObject AuthorizationCode;
    39.     public GameObject EmailActivation,PhoneVerificationOff;
    40.     public Image img1;
    41.     public GameObject VerifiedSymbo1,VerifiedTick;
    42.     Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
    43.     private TouchScreenKeyboard keyboard;
    44.     public Toggle musicToggle;
    45.     public Material GreyScaleOn;
    46.     public bool mobilecodecansent = false;
    47.  
    48.     public virtual void Start()
    49.     {
    50.  
    51.         keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.NumbersAndPunctuation);
    52.         Screen.orientation = ScreenOrientation.Portrait;
    53.        // spriteR = SpriteReplaced.GetComponent<SpriteRenderer>();
    54.         Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
    55.         {
    56.             dependencyStatus = task.Result;
    57.             if (dependencyStatus == Firebase.DependencyStatus.Available)
    58.             {
    59.                 InitializeFirebase();
    60.             }
    61.             else
    62.             {
    63.                 Debug.LogError(
    64.                 "Could not resolve all Firebase dependencies: " + dependencyStatus);
    65.             }
    66.         });
    67.     }
    68.  
    69.  
    70.     public void SetMusic()
    71.     {
    72.         if (musicToggle.isOn)
    73.         {
    74.             GoldOn.GetComponent<Image>().material = null;
    75.             mobilecodecansent = true;
    76.             Debug.Log("I am ON");
    77.         }
    78.         else
    79.         {
    80.             GoldOn.GetComponent<Image>().material = GreyScaleOn;
    81.             mobilecodecansent = false;
    82.             Debug.Log("I am Off");
    83.             // Set music Off
    84.         }
    85.     }
    86.  
    87.  
    88.  
    89.  
    90.     protected void InitializeFirebase()
    91.     {
    92.  
    93.         auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    94.         img1.fillAmount = 0;
    95.     }
    96.  
    97.  
    98.  
    99.  
    100.  
    101.  
    102.     protected bool LogTaskCompletion(Task task, string operation)
    103.     {
    104.         bool complete = false;
    105.         if (task.IsCanceled)
    106.         {
    107.         //   Button3.GetComponentInChildren<Text>().text = "Cancelled";
    108.  
    109.         }
    110.         else if (task.IsFaulted)
    111.         {
    112.         //    Button3.GetComponentInChildren<Text>().text = "Error";
    113.  
    114.             foreach (Exception exception in task.Exception.Flatten().InnerExceptions)
    115.             {
    116.                 string authErrorCode = "";
    117.                 Firebase.FirebaseException firebaseEx = exception as Firebase.FirebaseException;
    118.                 if (firebaseEx != null)
    119.                 {
    120.                 //    Button3.GetComponentInChildren<Text>().text = "AuthError";
    121.                     authErrorCode = String.Format("AuthError.{0}: ",
    122.                     ((Firebase.Auth.AuthError)firebaseEx.ErrorCode).ToString());
    123.                 }
    124.  
    125.             }
    126.         }
    127.         else if (task.IsCompleted)
    128.         {
    129.             VerifiedTick.SetActive(true);
    130.             //PhoneVerificationOff.SetActive(false);
    131.             //EmailActivation.SetActive(true);
    132.             img1.fillAmount = 0.5f;
    133.          
    134.            
    135.              Button3.GetComponentInChildren<Text>().text = "Verified";
    136.             Invoke("EmailActivation1", 4f);
    137.             complete = true;
    138.         }
    139.         return complete;
    140.     }
    141.  
    142.     public void EmailActivation1()
    143.     {
    144.         PhoneVerificationOff.SetActive(false);
    145.         EmailActivation.SetActive(true);
    146.         img1.fillAmount = 0.418f;
    147.  
    148.  
    149.     }
    150.  
    151.  
    152.     // Called when a sign-in without fetching profile data completes.
    153.     void HandleSignInWithUser(Task<Firebase.Auth.FirebaseUser> task)
    154.     {
    155.  
    156.         if (LogTaskCompletion(task, "Sign-in"))
    157.         {
    158.  
    159.         }
    160.     }
    161.  
    162.     // Called when a sign-in with profile data completes.
    163.     void HandleSignInWithSignInResult(Task<Firebase.Auth.SignInResult> task)
    164.     {
    165.  
    166.         if (LogTaskCompletion(task, "Sign-in"))
    167.         {
    168.  
    169.         }
    170.     }
    171.  
    172.  
    173.  
    174.     // Sign out the current user.
    175.     protected void SignOut()
    176.     {
    177.  
    178.         auth.SignOut();
    179.     }
    180.  
    181.     public void VerifyPhoneNumber2()
    182.     {
    183.         if (mobilecodecansent == true)
    184.         {
    185.  
    186.             string k2 = inp1.text.ToString();
    187.             string k = "+91" + k2;
    188.             Debug.Log("The Verify No is " + k);
    189.             var phoneAuthProvider = Firebase.Auth.PhoneAuthProvider.GetInstance(auth);
    190.             phoneAuthProvider.VerifyPhoneNumber(k, phoneAuthTimeoutMs, null,
    191.             verificationCompleted: (cred) =>
    192.             {
    193.             //    Button3.GetComponentInChildren<Text>().text = "verificationcompleted";
    194.  
    195.  
    196.             if (signInAndFetchProfile)
    197.                 {
    198.                     auth.SignInAndRetrieveDataWithCredentialAsync(cred).ContinueWith(
    199.             HandleSignInWithSignInResult);
    200.                 }
    201.                 else
    202.                 {
    203.                     auth.SignInWithCredentialAsync(cred).ContinueWith(HandleSignInWithUser);
    204.                 }
    205.             },
    206.             verificationFailed: (error) =>
    207.             {
    208.             //  Button3.GetComponentInChildren<Text>().text = "error";
    209.  
    210.         },
    211.             codeSent: (id, token) =>
    212.             {
    213.                 phoneAuthVerificationId = id;
    214.             //   Button3.GetComponentInChildren<Text>().text = "CodeSent";
    215.             Invoke("Time", 3f);
    216.  
    217.             },
    218.             codeAutoRetrievalTimeOut: (id) =>
    219.             {
    220.             // Button3.GetComponentInChildren<Text>().text = "Timeout";
    221.  
    222.         });
    223.        
    224.         }
    225.      
    226.     }
    227.  
    228.     public void Time()
    229.     {
    230.         GoldOn.SetActive(false);
    231.         AuthorizationCode.SetActive(true);
    232.         VerifiedSymbo1.SetActive(true);
    233.         GoldOn.SetActive(false);
    234.    
    235.     }
    236.  
    237.  
    238.  
    239.  
    240.  
    241.  
    242.  
    243.     public void VerifyReceivedPhoneCode2()
    244.     {
    245.         var phoneAuthProvider = Firebase.Auth.PhoneAuthProvider.GetInstance(auth);
    246.         // receivedCode should have been input by the user.
    247.  
    248.         string k = inp2.text.ToString();
    249.         Debug.Log("The Code for verification is" + k);
    250.         var cred = phoneAuthProvider.GetCredential(phoneAuthVerificationId, k);
    251.         if (signInAndFetchProfile)
    252.         {
    253.             auth.SignInAndRetrieveDataWithCredentialAsync(cred).ContinueWith(
    254.             HandleSignInWithSignInResult);
    255.         }
    256.         else
    257.         {
    258.             auth.SignInWithCredentialAsync(cred).ContinueWith(HandleSignInWithUser);
    259.         }
    260.     }
    261.  
    262.  
    263.  
    264.  
    265. }
    266.  
    267.  
    268.  
    269.  
    270.  
    271.  
    272.  
     
  4. satyam1507

    satyam1507

    Joined:
    Oct 22, 2016
    Posts:
    3
    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.
     
  5. NubieUnity

    NubieUnity

    Joined:
    May 9, 2021
    Posts:
    1
    @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.