Android Fire-Base OTP无法验证

时间:2019-03-09 13:44:21

标签: android firebase firebase-authentication one-time-password

我制作了一个读取OTP并使用手机号码登录的应用程序,就像在第一个屏幕中,用户必须输入手机号码并在otp发送给用户并继续进行第二次活动时继续操作。在这里,用户将输入otp,该otp将进入第一个活动,最后我的应用程序未验证otp,请帮助我

 public class LoginActivity extends AppCompatActivity {
EditText phonetextview;
String phone_number;
FirebaseAuth auth;
    private FirebaseAuth mAuth;
    private PhoneAuthProvider.ForceResendingToken mResendToken;
private  String verificationCode;
    private String mVerificationId;


PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private static final String TAG ="MainActivity";
String verificationId;
String code;
//    final PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    mAuth = FirebaseAuth.getInstance();


   //






    Intent intent = getIntent();
    final String otp = intent.getStringExtra("otp");

    Toast.makeText(getBaseContext(),otp,Toast.LENGTH_SHORT).show();







    //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);


    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            Toast.makeText(getBaseContext(),"hoooooooooo",Toast.LENGTH_SHORT).show();

            signInWithPhoneAuthCredential(credential);



        }

        private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {


            System.exit(0);

        }


        @Override
        public void onVerificationFailed(FirebaseException e) {
            {
                e.printStackTrace();

                Toast toast = Toast.makeText(getApplicationContext(), (CharSequence) e,
                        Toast.LENGTH_SHORT);
                toast.show();

            }

        }


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



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

            mVerificationId = verificationId;
            mResendToken = token;

            Intent i = new Intent(getApplicationContext(),Otp.class);
            startActivity(i);


           // View v = (View) findViewById(R.id.phonetextview);
            //((ViewManager)v.getParent()).removeView(v);
            //View vv = (View) findViewById(R.id.cardView);
            //((ViewManager)vv.getParent()).removeView(vv);







        }



    };

    CardView card_view = findViewById(R.id.cardView);
    card_view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {






            phonetextview =findViewById(R.id.phonetextview);
            phone_number = PhoneNumberUtils.formatNumber(phonetextview.getText().toString());

            send();



            Toast toast = Toast.makeText(getApplicationContext(), phone_number,
                    Toast.LENGTH_SHORT);
            toast.show();



        }


    });

}

private void send() {

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
           "+91"+phone_number,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);             // ForceResendingToken from callbacks


}
    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            Toast.makeText(getBaseContext(),"hoooooooooo",Toast.LENGTH_SHORT).show();

                            FirebaseUser user = task.getResult().getUser();
                            // [START_EXCLUDE]

                            // [END_EXCLUDE]
                        } else {
                            // Sign in failed, display a message and update the UI
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                // The verification code entered was invalid
                                // [START_EXCLUDE silent]
                                //mVerificationField.setError("Invalid code.");
                                // [END_EXCLUDE]
                            }
                            // [START_EXCLUDE silent]
                            // Update UI
                          //  updateUI(STATE_SIGNIN_FAILED);
                            // [END_EXCLUDE]
                        }
                    }
                });
    }


}

这是我的otp输入活动

 public class Otp extends AppCompatActivity {

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





    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {



            EditText fd = (EditText) findViewById(R.id.et1);
            String value= fd.getText().toString();
  //        int finalValue=Integer.parseInt(value);

            final EditText sd = (EditText) findViewById(R.id.et2);
            String value1= sd.getText().toString();
            // int finalValue1=Integer.parseInt(value1);

            EditText td = (EditText) findViewById(R.id.et3);
            String value2= td.getText().toString();
            // int finalValue2=Integer.parseInt(value2);

            EditText fod = (EditText) findViewById(R.id.et4);
            String value3= fod.getText().toString();
            //int finalValue3=Integer.parseInt(value3);

            EditText fid = (EditText) findViewById(R.id.et5);
            String value4= fid.getText().toString();
            //int finalValue4=Integer.parseInt(value4);

            EditText sid = (EditText) findViewById(R.id.et6);
            String value5= sid.getText().toString();
            //int finalValue5=Integer.parseInt(value5);




            final String otp = value + value1+value2+value3+value4+value5;



            Intent intent = new Intent(v.getContext(), LoginActivity.class);
            intent.putExtra("otp", otp);
            startActivity(intent);


        finish();





        }
    });


}
}

1 个答案:

答案 0 :(得分:0)

如果您的问题不是无法获得OTP,则解决方案是将SHA-1指纹添加到Firebase项目中。这是很多人无法从Firebase获取OTP的常见问题