我正在 android studio 中制作一个简单的聊天应用程序,但即使我使用真实设备运行该应用程序(三星 Galaxy a52 android 14),电话号码 otp 也无法发送
这是代码:
package com.daffakhairy.easychat;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.daffakhairy.easychat.utils.AndroidUtil;
import com.google.firebase.Firebase;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthOptions;
import com.google.firebase.auth.PhoneAuthProvider;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class LoginOtpActivity extends AppCompatActivity {
String phoneNumber;
Long timeoutSeconds = 60L;
String verificationCode;
PhoneAuthProvider.ForceResendingToken resendingToken;
EditText otpInput;
Button nextBtn;
ProgressBar progressBar;
TextView resendOtpTextView;
FirebaseAuth mAuth = FirebaseAuth.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_login_otp);
otpInput = findViewById(R.id.login_otp);
nextBtn = findViewById(R.id.login_next_btn);
progressBar = findViewById(R.id.login_progress_bar);
resendOtpTextView = findViewById(R.id.resend_otp_textview);
phoneNumber = getIntent().getExtras().getString("phone");
sendOtp(phoneNumber, false);
}
void sendOtp(String phoneNumber,boolean isResend){
setInProgress(true);
PhoneAuthOptions.Builder builder =
PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber(phoneNumber)
.setTimeout(timeoutSeconds, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(new
PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
signIn(phoneAuthCredential);
setInProgress(false);
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
AndroidUtil.showToast(getApplicationContext(),"OTP verification failed");
setInProgress(false);
}
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationCode = s;
resendingToken = forceResendingToken;
AndroidUtil.showToast(getApplicationContext(),"OTP sent successfully");
setInProgress(false);
}
});
if(isResend){
PhoneAuthProvider.verifyPhoneNumber(builder.setForceResendingToken(resendingToken).build());
}else{
PhoneAuthProvider.verifyPhoneNumber(builder.build());
}
}
void setInProgress(boolean inProgress){
if(inProgress){
progressBar.setVisibility(View.VISIBLE);
nextBtn.setVisibility(View.GONE);
}else {
progressBar.setVisibility(View.GONE);
nextBtn.setVisibility((View.VISIBLE));
}
}
void signIn(PhoneAuthCredential phoneAuthCredential){
//login and go to next activity
}
}
这是 logcat 中的错误:
Failed to initialize reCAPTCHA config: No Recaptcha Enterprise siteKey configured for tenant/project *
[SmsRetrieverHelper] SMS verification code request failed: unknown status code: 17499 BILLING_NOT_ENABLED
我还没有超越 10 个限制,添加了 sha 1 密钥,并添加了所需的所有依赖项
在 Firebase 中启用计费
Go to the Firebase Console
Select your project.
In the left sidebar, click on Project Settings (the gear icon).
Under Billing, you will see the option to enable billing for your Firebase project.
If you haven't already, you need to link a billing account to your project.
If you don't have a billing account, you'll be prompted to create one in Google Cloud.
Important Notes:
You don’t need to exceed free-tier usage to enable billing; however, enabling billing is required to use SMS verification services.
After enabling billing, Firebase will give you a $300 free usage credit for 90 days.
检查短信验证的 Firebase 定价
Firebase offers a free quota for SMS verifications (the free tier), but once that quota is exhausted, you will need to enable billing to continue using SMS.
You can review the pricing for Firebase Authentication’s phone number sign-in service here.
验证账单设置
启用结算功能后,请转到 Google Cloud Console 并验证您的项目是否已启用结算功能。这可确保 Firebase 可以使用 SMS 服务,而不会遇到 BILLING_NOT_ENABLED 等问题。