簡體   English   中英

Firebase 電話認證只發送 OTP 用於測試電話號碼

[英]Firebase Phone authentication only sending OTP For Testing Phone Numbers

我在我的項目中使用了 Firebase Phone Auth。 但是 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();
        }
    };
}

}

請給我一個解決方案。

如果有人遇到這個問題,請嘗試將 SHA 指紋添加到您的 firebase 以接收短信

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM