我正在尝试制作一个仅具有手机号码登录功能的应用程序。我在java中使用firebase auth、firestore和android studio来制作它,我有5个活动,主页(仅限Splash)>登录电话号码>登录OTP>输入用户名>主屏幕
用户在第二个活动中使用国家/地区代码选择器输入手机号码,然后发送 OTP,用户在第三个活动中输入收到的 OTP,在第四个活动中输入用户名。如果 otp 已验证并被重定向到所谓的主屏幕。
我使用“+911234567890”作为测试电话号码和otp“111111”,当我用我的应用程序测试运行它时,一切都工作得很好,所有的祝酒词和代码都很好。
但是当我尝试使用我自己的实际电话号码“+919811226XXX”运行我的应用程序时,然后在 LoginOTP 活动中我收到祝酒词,Firebase 发生内部错误 [BILLING_NOT_ENABLED],在网上我找不到任何解决方案,有些说要激活一些高级计划每天可以发送更多短信,但在免费中我也有每天 10 条短信的限制,我什至没有使用其中 1 条,那么为什么会出现此错误,有人说启用谷歌云计费帐户,但是为此,我需要信用卡详细信息和所有信息,作为一个孩子和学生,我没有钱,也没有卡或任何其他可验证的付款选项。
这是我的 LoginPhoneNumberActivity.java
package com.example.pchat;
public class LoginPhoneNumberActivity extends AppCompatActivity {
CountryCodePicker countryCodePicker;
EditText phoneInput;
Button sendOtpBtn;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_login_phone_number);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.phone_login_screen), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
countryCodePicker = findViewById(R.id.login_countrycode);
phoneInput = findViewById(R.id.login_mobile_number);
sendOtpBtn = findViewById(R.id.send_otp_btn);
progressBar = findViewById(R.id.login_progress_bar);
progressBar.setVisibility(View.GONE);
countryCodePicker.registerCarrierNumberEditText(phoneInput);
sendOtpBtn.setOnClickListener((v)->{
if(!countryCodePicker.isValidFullNumber()){
phoneInput.setError("Phone number not valid");
return;
}
Intent intent = new Intent(LoginPhoneNumberActivity.this,LoginOtpActivity.class);
intent.putExtra("phone",countryCodePicker.getFullNumberWithPlus());
startActivity(intent);
});
}
}
我的 LoginOtpActivity.java 如下 -
package com.example.pchat;
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);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.login_otp_activity), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
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 = Objects.requireNonNull(getIntent().getExtras()).getString("phone");
sendOtp(phoneNumber,false);
nextBtn.setOnClickListener(v -> {
String enteredOtp = otpInput.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCode,enteredOtp);
signIn(credential);
});
resendOtpTextView.setOnClickListener((v)->{
sendOtp(phoneNumber,true);
});
}
void sendOtp(String phoneNumber,boolean isResend){
startResendTimer();
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) {
AndroidUtil.showToast(getApplicationContext(), "OTP verified successfully!");
signIn(phoneAuthCredential);
setInProgress(false);
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
String Fexceptions = e.getMessage();
AndroidUtil.showToast(getApplicationContext(), Fexceptions);
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
setInProgress(true);
mAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
setInProgress(false);
if(task.isSuccessful()){
Intent intent = new Intent(LoginOtpActivity.this,LoginUsernameActivity.class);
intent.putExtra("phone",phoneNumber);
startActivity(intent);
finish();
}else{
AndroidUtil.showToast(getApplicationContext(),"OTP verification failed");
}
}
});
}
void startResendTimer(){
resendOtpTextView.setEnabled(false);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@SuppressLint("DefaultLocale")
@Override
public void run() {
timeoutSeconds--;
resendOtpTextView.setText(String.format("Resend OTP after %d seconds", timeoutSeconds));
if(timeoutSeconds<=0){
timeoutSeconds =60L;
timer.cancel();
runOnUiThread(() -> {
resendOtpTextView.setEnabled(true);
});
}
}
},0,1000);
}
}
我的 LoginUsernameActivity.java 看起来有点像下面这样。
package com.example.pchat;
public class LoginUsernameActivity extends AppCompatActivity {
EditText usernameInput;
Button letMeInBtn;
ProgressBar progressBar;
String phoneNumber;
UserModel userModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_login_username);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.login_username_activity), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
usernameInput = findViewById(R.id.login_username);
letMeInBtn = findViewById(R.id.login_let_me_in_btn);
progressBar =findViewById(R.id.login_progress_bar);
phoneNumber = Objects.requireNonNull(getIntent().getExtras()).getString("phone");
getUsername();
letMeInBtn.setOnClickListener((v -> {
setUsername();
}));
}
void setUsername(){
String username = usernameInput.getText().toString();
if(username.isEmpty() || username.length()<3){
usernameInput.setError("Username length should be at least 3 chars");
return;
}
setInProgress(true);
if(userModel!=null){
userModel.setUsername(username);
}else{
userModel = new UserModel(phoneNumber,username, Timestamp.now(),FirebaseUtil.currentUserId());
}
FirebaseUtil.currentUserDetails().set(userModel).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
setInProgress(false);
if(task.isSuccessful()){
Intent intent = new Intent(LoginUsernameActivity.this,HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(intent);
finish();
}
}
});
}
void getUsername(){
setInProgress(true);
FirebaseUtil.currentUserDetails().get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
setInProgress(false);
if(task.isSuccessful()){
userModel = task.getResult().toObject(UserModel.class);
if(userModel!=null){
usernameInput.setText(userModel.getUsername());
}
}
}
});
}
void setInProgress(boolean inProgress){
if(inProgress){
progressBar.setVisibility(View.VISIBLE);
letMeInBtn.setVisibility(View.GONE);
}else{
progressBar.setVisibility(View.GONE);
letMeInBtn.setVisibility(View.VISIBLE);
}
}
}
请帮帮我
我想使用 firebase auth 制作一个基本的手机登录应用程序,它在测试电话号码上运行得很好,但对于真实的电话号码显示未启用计费。怎么办,我没有钱投入这个项目。 此外,早些时候它曾经可以正常工作,没有任何问题,相同的代码,就在 1-2 年前,除了这个错误之外,一切都曾经是相同的......
您收到
BILLING_NOT_ENABLED
错误,因为您的 Firebase 项目属于 Spark 计划(免费)。因此,这不是关于您共享的代码,而是关于 2 个月前所做的更改。所以根据这个GitHub问题:
这意味着您必须将您的项目明确链接到 Cloud Billing 帐户。但是,在 Spark 计划中,您应该仍然可以根据其自 2024 年 9 月 1 日起,所有使用 Firebase 电话身份验证 (SMS) 的项目都必须链接到 Cloud Billing 帐户
定价页面每天发送 10 条免费短信。