25
votes

According to Firebase documentation (https://firebase.google.com/docs/auth/android/phone-auth#send-a-verification-code-to-the-users-phone), there is callback for handling the phone number authentication.

mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

    @Override
    public void onVerificationCompleted(PhoneAuthCredential credential) {

        Log.d(TAG, "onVerificationCompleted:" + credential);
        signInWithPhoneAuthCredential(credential);
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {

        Log.w(TAG, "onVerificationFailed", e);
    }

    @Override
    public void onCodeSent(String verificationId,
                           PhoneAuthProvider.ForceResendingToken token) {

        Log.d(TAG, "onCodeSent:" + verificationId);

        // Save verification ID and resending token so we can use them later
        mVerificationId = verificationId;
        mResendToken = token;
    }
};

My question is on onCodeSent method. It said on the doc here (https://firebase.google.com/docs/reference/android/com/google/firebase/auth/PhoneAuthProvider.ForceResendingToken)

that the token can be used to force re-sending an SMS verification code. However, after doing some research on the doc I still don't know how.

I would like to ask how to use this token to resend the SMS verification ?

4
Sure thanks for helping. :)Boonya Kitpitak

4 Answers

45
votes

Source: Firebase Quickstarts for Android

This is the method used to resend SMS Verifications.

private void resendVerificationCode(String phoneNumber,
                                    PhoneAuthProvider.ForceResendingToken token) {
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks,         // OnVerificationStateChangedCallbacks
            token);             // ForceResendingToken from callbacks
}
1
votes

you can use a Firebase method to resend verification code as say PERSISTENCE and intercept sms code for to check in automatic, for instance while run a progress dialog, and transparent to the user, simply which

// [START resend_verification]
public void resendVerificationCode(String phoneNumber,
                                   PhoneAuthProvider.ForceResendingToken token) {
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            activity,           //a reference to an activity if this method is in a custom service
            mCallbacks,
            token);        // resending with token got at previous call's `callbacks` method `onCodeSent` 
    // [END start_phone_auth]
}

check the sms with broadcast receiver in a fragment

private BroadcastReceiver smsBroadcastReceiver;
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
public static final String SMS_BUNDLE = "pdus";

 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    smsBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("smsBroadcastReceiver", "onReceive");
            Bundle pudsBundle = intent.getExtras();
            Object[] pdus = (Object[]) pudsBundle.get(SMS_BUNDLE);
            SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
            Log.i(TAG,  messages.getMessageBody());

            firebaseVerificationCode = messages.getMessageBody().trim().split(" ")[0];//only a number code 
            Toast.makeText(getContext(), firebaseVerificationCode,Toast.LENGTH_SHORT).show();
            String token = firebaseAutenticationService.getVerificationCode();//your service
        firebaseAutenticationService.verifyPhoneNumberWithCode(token,verificationCode);
        }
    };
}
1
votes

Updated code

fun resendOTP(activity: Activity,mobileNo: String){
    val options = PhoneAuthOptions.newBuilder(auth)
        .setPhoneNumber(mobileNo) // Phone number to verify
        .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
        .setActivity(activity) // Activity (for callback binding)
        .setCallbacks(callback) // OnVerificationStateChangedCallbacks
        .setForceResendingToken(resendToken!!) // ForceResendingToken from callbacks
        .build()
    PhoneAuthProvider.verifyPhoneNumber(options)
}

For java see Github

Thanks to GGWP for link .

0
votes

final FirebaseAuth _auth = FirebaseAuth.instance;

  void resendVerificationCode(String phoneNumber) async {
    print(widget.forceResendingToken);
    EasyLoading.show();
    await _auth.verifyPhoneNumber(
        phoneNumber: "+91" + phoneNumber,
        forceResendingToken: widget.forceResendingToken,
        timeout: const Duration(seconds: 120),
        verificationCompleted: (PhoneAuthCredential phoneAuthCredential) async {
          await _auth.signInWithCredential(phoneAuthCredential);
          EasyLoading.dismiss();
          showSnackbar(
              "Phone number automatically verified and user signed in: ${_auth.currentUser.uid}");
        },
        verificationFailed: (FirebaseAuthException authException) {
          EasyLoading.dismiss();
          showSnackbar(
              'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
        },
        codeSent: (String verificationId, [int forceResendingToken]) async {
          print("TESTED 1.D");
          EasyLoading.dismiss();
          showSnackbar('Please check your phone for the verification code.');
          widget._verificationId = verificationId;
        },
        codeAutoRetrievalTimeout: (String verificationId) {
          EasyLoading.dismiss();
          showSnackbar("verification code: " + verificationId);
          widget._verificationId = verificationId;
        });
  }

You will get forceResendToken while sending the SMS on very first time. Just save it to variable and pass that variable next time.

// forceResendingToken: widget.forceResendingToken