How to resend SMS verification in Firebase Phone Authentication Android?

前端 未结 3 923
梦如初夏
梦如初夏 2021-01-07 19:41

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

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-07 19:55

    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);
            }
        };
    }
    

提交回复
热议问题