Java Firebase未正确发送otp
我正在开发一个应用程序,它具有获取OTP的功能,我使用了firebase OTP功能(使用电话号码在Android上与firebase进行身份验证),但不幸的是,有时我会面临这样的问题:我没有从firebase获得OTP,同时它也没有给出任何错误。我不知道我犯了什么错误。需要帮助来解决这个问题。我也尝试了一些示例,它们工作正常,但当我尝试集成到我的项目中时,firebase没有发送otp。我希望将手机号码发送到firestore,用户当前的国家代码为ex(+91,+888) 这是我迄今为止在项目中尝试的:Java Firebase未正确发送otp,java,android,Java,Android,我正在开发一个应用程序,它具有获取OTP的功能,我使用了firebase OTP功能(使用电话号码在Android上与firebase进行身份验证),但不幸的是,有时我会面临这样的问题:我没有从firebase获得OTP,同时它也没有给出任何错误。我不知道我犯了什么错误。需要帮助来解决这个问题。我也尝试了一些示例,它们工作正常,但当我尝试集成到我的项目中时,firebase没有发送otp。我希望将手机号码发送到firestore,用户当前的国家代码为ex(+91,+888) 这是我迄今为止在项目
package com.example.NewActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.aayushchaubey.meetdax.R;
import com.example.activity.HomeActivity;
import com.example.activity.PhoneNumberLogin;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.FirebaseTooManyRequestsException;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseAuthSettings;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class LoginWithMobileNumber extends AppCompatActivity {
EditText phoneNo_Edt;
Button loginBtn;
FirebaseAuth firebaseAuth;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private static final String TAG = "FirebasePhoneNumAuth";
PhoneAuthProvider.ForceResendingToken mResendToken;
String mVerificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mobileno_verification);
phoneNo_Edt=(EditText)findViewById(R.id.phoneNoEdt);
loginBtn=(Button)findViewById(R.id.phoneNoLoginBtn);
phoneNo_Edt.setSingleLine();
firebaseAuth= FirebaseAuth.getInstance();
addOnClickListener();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// This callback will be invoked in two situations:
// 1 - Instant verification. In some cases the phone number can be instantly
// verified without needing to send or enter a verification code.
// 2 - Auto-retrieval. On some devices Google Play services can automatically
// detect the incoming verification SMS and perform verification without
// user action.
Log.d(TAG, "onVerificationCompleted:" + credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
// This callback is invoked in an invalid request for verification is made,
// for instance if the the phone number format is not valid.
Log.w(TAG, "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
// ...
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
// ...
}
// Show a message and update the UI
// ...
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
// The SMS verification code has been sent to the provided phone number, we
// now need to ask the user to enter the code and then construct a credential
// by combining the code with a verification ID.
Log.d(TAG, "onCodeSent:" + verificationId);
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
// ...
}
};
}
private void addOnClickListener() {
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendSms();
}
});
}
public void sendSms(){
String phoneNumber=phoneNo_Edt.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
firebaseAuth.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");
FirebaseUser user = task.getResult().getUser();
if(mVerificationId!=null){
Intent intent = new Intent(getApplicationContext(), VerifyOtp.class);
intent.putExtra("verificationcode",mVerificationId);
startActivity(intent); }
// ...
} 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
}
}
}
});
}
}
package com.example.NewActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.annotation.NonNull;
导入android.support.v7.app.AppActivity;
导入android.text.TextUtils;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
导入com.example.aayushchaubey.meetdax.R;
导入com.example.activity.HomeActivity;
导入com.example.activity.PhoneNumberLogin;
导入com.google.android.gms.tasks.OnCompleteListener;
导入com.google.android.gms.tasks.Task;
导入com.google.firebase.FirebaseException;
导入com.google.firebase.FirebaseTooManyRequestsException;
导入com.google.firebase.auth.AuthCredential;
导入com.google.firebase.auth.AuthResult;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.auth.FirebaseAuthException;
导入com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
导入com.google.firebase.auth.FirebaseAuthSettings;
导入com.google.firebase.auth.FirebaseUser;
导入com.google.firebase.auth.PhoneAuthCredential;
导入com.google.firebase.auth.PhoneAuthProvider;
导入java.util.concurrent.TimeUnit;
公共类LoginWithMobileNumber扩展了AppCompatActivity{
编辑文本电话号码;
按钮登录;
FirebaseAuth FirebaseAuth;
PhoneAuthProvider.OnVerificationStateChangedCallbacks McCallbacks;
私有静态最终字符串标记=“FirebasePhoneNumAuth”;
PhoneAuthProvider.ForceResendingToken mResendToken;
字符串mvericationid;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mobileno_验证);
phoneNo_Edt=(EditText)findViewById(R.id.phoneNoEdt);
loginBtn=(按钮)findViewById(R.id.phoneNoLoginBtn);
电话号码设置单行();
firebaseAuth=firebaseAuth.getInstance();
addOnClickListener();
mCallbacks=新的PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
@凌驾
已完成验证的公共无效(PhoneAuthCredential凭据){
//此回调将在两种情况下调用:
//1-即时验证。在某些情况下,电话号码可以即时验证
//已验证,无需发送或输入验证代码。
//2-自动检索。在某些设备上,Google Play服务可以自动
//检测传入的验证SMS并执行验证,无需
//用户操作。
Log.d(标签“onVerificationCompleted:”+凭证);
使用PhoneAuthCredential(凭证)登录;
}
@凌驾
public void onVerificationFailed(FirebaseException e){
//在发出无效验证请求时调用此回调,
//例如,如果电话号码格式无效。
Log.w(标签“onVerificationFailed”,e);
if(例如FirebaseAuthInvalidCredentialsException实例){
//无效请求
// ...
}else if(例如FirebaseTooManyRequestsException的实例){
//已超过项目的SMS配额
// ...
}
//显示消息并更新UI
// ...
}
@凌驾
public void oncodesnt(字符串验证ID,
PhoneAuthProvider.ForceResendingToken(令牌){
//短信验证码已发送至提供的电话号码,我们
//现在需要要求用户输入代码,然后构造凭证
//通过将代码与验证ID相结合。
Log.d(标签“oncodesnt:+验证ID”);
//保存验证ID和重新发送令牌,以便稍后使用
mvericationId=验证ID;
mResendToken=令牌;
// ...
}
};
}
私有void addOnClickListener(){
loginBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
sendSms();
}
});
}
公共无效发送SMS(){
字符串phoneNumber=phoneNo_Edt.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber,//要验证的电话号码
60,//超时持续时间
TimeUnit.SECONDS,//超时单位
此,//活动(用于回调绑定)
mCallbacks);//OnVerificationStateChangedCallbacks
}
使用PhoneAuthCredential(PhoneAuthCredential凭据)的专用无效登录{
firebaseAuth.signInWithCredential(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用已登录用户更新用户界面
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
// In case OTP is received
if (code != null) {
otpTxt.setText(code);
verifyCode(code);
} else {
// In case OTP is not received
signInWithCredentials(phoneAuthCredential);
}
}