android Firebase电话认证仅发送OTP用于测试电话号码

rsaldnfx  于 2023-01-03  发布在  Android
关注(0)|答案(1)|浏览(184)

我在我的项目中使用了Firebase电话认证。但是Firebase只发送OTP来测试电话号码。
我已经在Firebase项目中添加了SHA1和SHA256,并启用了Android DeviceCheck API。但一直没有从Firebase获得OTP。
当我检查测试电话号码的OTP弹出屏幕显示出来,我进入了OTP,我创建了验证码,即123456,111111,222222等,但除此之外,我没有得到从Firebase的OTP。
这是我的密码

username = findViewById(R.id.username);
    fullname = findViewById(R.id.fullname);
    mTelephoneNumber = findViewById(R.id.telephonenumberregister);
    continueregister = findViewById(R.id.continueregister);
    back = findViewById(R.id.back);
    txt_login = findViewById(R.id.btnSign);
    countryCodePicker = findViewById(R.id.countrycodepicker);

    pd = new ProgressDialog(RegisterActivity.this);
    pd.setCancelable(false);
    auth = FirebaseAuth.getInstance();

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(RegisterActivity.this, StartActivity.class));
        }
    });
    txt_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

    continueregister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pd.setMessage("Loading");
            pd.show();
            mFullTelephoneNumber = countryCodePicker.getSelectedCountryCodeWithPlus() + mTelephoneNumber.getText().toString();
            FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("telephoneno").equalTo(mFullTelephoneNumber)
                    .addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            if (snapshot.exists()){
                                pd.dismiss();
                                Toast.makeText(RegisterActivity.this, "There is a user who has this phone number", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                pd.dismiss();
                                phoneVerification();
                            }

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });
        }
    });
}

private void phoneVerification(){
    pd.setMessage("Sending");
    pd.show();

    AlertDialog dialog;

    View verficationView = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.verificationdialoglayout, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
    builder.setView(verficationView);
    builder.setCancelable(false);
    dialog = builder.create();

    final EditText input_verificationCode = verficationView.findViewById(R.id.mverificationcode);
    Button submit = verficationView.findViewById(R.id.submit);
    Button resend = verficationView.findViewById(R.id.resend);
    TextView countDownView = verficationView.findViewById(R.id.countdown);
    ImageButton closeVerification = verficationView.findViewById(R.id.closeverification);

    Log.d("a", mFullTelephoneNumber);

    CountDownTimer countDownTimer = countDownTimer(countDownView, dialog);

    closeVerification.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
            countDownTimer.onFinish();
        }
    });

    callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {
            Log.d("error", e.getMessage());
        }

        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            Log.d("verificationcode", s);
            mVerificationId = s;
            resendingToken = forceResendingToken;
            pd.dismiss();
            dialog.show();
            countDownTimer.start();

        }
    };

    PhoneAuthOptions options =
            PhoneAuthOptions.newBuilder(firebaseAuth)
                    .setPhoneNumber(mFullTelephoneNumber)
                    .setCallbacks(callbacks)
                    .setTimeout(60L, TimeUnit.SECONDS)
                    .setActivity(RegisterActivity.this)
                    .build();
    PhoneAuthProvider.verifyPhoneNumber(options);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pd.setMessage("Please Wait...");
            pd.show();

            String str_verificationCode = input_verificationCode.getText().toString();

            if (!TextUtils.isEmpty(str_verificationCode) && str_verificationCode.length() == 6){
                PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, input_verificationCode.getText().toString());
                firebaseAuth.signInWithCredential(credential).addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()){
                            String userID = task.getResult().getUser().getUid();
                            String a = "0";
                            DatabaseReference pref = FirebaseDatabase.getInstance().getReference().child("wallet").child(userID);
                            HashMap<String, Object> point = new HashMap<>();
                            point.put("balance", a);
                            pref.setValue(point);
                            reference = FirebaseDatabase.getInstance().getReference().child("Users").child(userID);
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("id", userID);
                            map.put("telephoneno", mFullTelephoneNumber);
                            map.put("username", username.getText().toString().toLowerCase());
                            map.put("fullname", fullname.getText().toString());
                            map.put("imageurl", "https://firebasestorage.googleapis.com/v0/b/instagramtest-fcbef.appspot.com/o/placeholder.png?alt=media&token=b09b809d-a5f8-499b-9563-5252262e9a49");
                            map.put("bio", "");

                            reference.setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()){
                                        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        pd.dismiss();
                                        countDownTimer.onFinish();

                                    }
                                    else {
                                        Toast.makeText(RegisterActivity.this, "error", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                        else {
                            pd.dismiss();
                            dialog.dismiss();
                            Toast.makeText(RegisterActivity.this, "Wrong code is entered.", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
            else {
                Toast.makeText(RegisterActivity.this, "Please enter the code!", Toast.LENGTH_SHORT).show();

            }
        }
    });

    resend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
            pd.setMessage("Resending");
            pd.show();

            countDownTimer.onFinish();
            countDownTimer.start();

            PhoneAuthOptions options =
                    PhoneAuthOptions.newBuilder(firebaseAuth)
                            .setPhoneNumber(mFullTelephoneNumber)
                            .setCallbacks(callbacks)
                            .setTimeout(60L, TimeUnit.SECONDS)
                            .setActivity(RegisterActivity.this)
                            .setForceResendingToken(resendingToken)
                            .build();
            PhoneAuthProvider.verifyPhoneNumber(options);
        }
    });
}

private CountDownTimer countDownTimer(TextView countDownView, AlertDialog dialog){
    return new CountDownTimer(61000, 1000){
        @SuppressLint("SetTextI18n")
        @Override
        public void onTick(long l) {
            countDownView.setVisibility(View.VISIBLE);
            countDownView.setText("" + l / 1000);
        }

        @Override
        public void onFinish() {
            countDownView.setVisibility(View.GONE);
            dialog.dismiss();
        }
    };
}

}
请给予我一个解决办法。

falq053o

falq053o1#

如果有人面临这个问题,尝试添加SHA指纹到您的firebase接收短信

相关问题