phoneauthprovider 的 oncodesent 不起作用

use*_*311 5 android firebase firebase-authentication

所以我一直在尝试解决这个问题,但我似乎不知道出了什么问题。我有一个按钮,单击时会调用 PhoneAuthProvider,它有 4 个选项:OnVerifiicationCompleted、OnVerificationFailed、OnCodeSent 和 onCodeAutoRetrieval。问题是 oncodesent 正在被调用,但我放入其中的代码不起作用,甚至调试日志也不起作用。我收到的只是一条显示验证码的短信。当我为onCodeSent的verificationID设置一个字符串值时,该字符串的值为:null。这是我的代码:

sendLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popSound.start();

            if (greenCheck.getVisibility() == View.VISIBLE) {
                //send link

                // we will finish this activity and send the link to the number
                // an option to resend code to the number: it will be provided at the link
                // in this format: resend code to (XXX)-XxX-XXXX
                String number = phoneNumber.getText().toString();
                phoneNumber.setText("");
                sendLink.setEnabled(false);

                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        number,
                        60,
                        TimeUnit.SECONDS,
                        MobileNumberActivity.this,
                        new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                            @Override
                            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                                signInWithPhoneAuthCredential(phoneAuthCredential);
                            }

                            @Override
                            public void onVerificationFailed(FirebaseException e) {

                            }

                            @Override
                            public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                                super.onCodeSent(s, forceResendingToken);
                                // verificationID never gets assigned s and it is null. log.d isn't on logcat
                                verificationID = s;
                                Log.d("testing", "onCodeSent: " + verificationID);
                            }

                            @Override
                            public void onCodeAutoRetrievalTimeOut(String s) {
                                super.onCodeAutoRetrievalTimeOut(s);
                            }
                        }
                );


                Intent i = new Intent(MobileNumberActivity.this, VerificationActivity.class);
                startActivity(i);

                finish();
            } else if (phoneNumber.getText().toString().length() == 0) {
                Toast.makeText(MobileNumberActivity.this, "Please enter a phone number", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MobileNumberActivity.this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show();
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

use*_*311 -2

经过反复试验终于找到了答案。我不太清楚为什么这会起作用,但我的猜测是因为因为我正在创建 PhoneAuthProvider 的实例,所以它只在 oncreate 中起作用。我在 oncreate 中有我的,但它被 setOnClickListener 包围,正如您在上面的代码中看到的那样。因此,对我有用的是启动一个意图,将其定向到我的验证活动,在其 oncreate 方法中,我仅自行创建了一个 PhoneAuthProvider 实例,并且它起作用了。

第一个活动:

sendLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popSound.start();

            if (greenCheck.getVisibility() == View.VISIBLE) {
                //send link

                number = phoneNumber.getText().toString();
                phoneNumber.setText("");
                sendLink.setEnabled(false);



                Intent i = new Intent(MobileNumberActivity.this, VerificationActivity.class);
                startActivity(i);



                finish();
            } else if (phoneNumber.getText().toString().length() == 0) {
                Toast.makeText(MobileNumberActivity.this, "Please enter a phone number", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MobileNumberActivity.this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show();
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

第二项活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verification);


    mAuth = FirebaseAuth.getInstance();



    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verification without
            //     user action.
            Log.d("COmpleted", "onVerificationCompleted:" + credential);

            signInWithPhoneAuthCredential(credential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            Log.w("failed", "onVerificationFailed", e);

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // ...
            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // ...
            }

            // Show a message and update the UI
            // ...
        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.
            Log.d("codesent", "onCodeSent:" + verificationId);

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

            // ...
        }
    };


    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            MobileNumberActivity.number,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);
}
Run Code Online (Sandbox Code Playgroud)