package com.example.XX;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
public class SignInFragment extends Fragment {
private static final String TAG ="INSIDE_SIGN_IN_FRAGMENT" ;
FirebaseAuth mAuth;
EditText xPhone;
EditText xOTP;
String codeSent;
public SignInFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sign_in, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button getOTP=view.findViewById(R.id.button_xOTP);
Button submitButton=view.findViewById(R.id.signInbutton);
xPhone=view.findViewById(R.id.editText_xMobile);
xOTP=view.findViewById(R.id.editText_xOTP);
mAuth=FirebaseAuth.getInstance();
getOTP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendVerificationCode();
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
verifyOTP();
}
});
}
private void verifyOTP(){
String tOTP=xOTP.getText().toString().trim();
if(tOTP.isEmpty()){
xOTP.setError("OTP is required!");
xOTP.requestFocus();
return;
}
if(tOTP.length()<6){
xOTP.setError("Please enter a valid OTP!");
xOTP.requestFocus();
return;
}
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, tOTP);
signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener((Executor) 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();
// ...
} 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
}
}
}
});
}
private void sendVerificationCode() {
String xMobileNumber=xPhone.getText().toString().trim();
if(xMobileNumber.isEmpty()){
xPhone.setError("Phone number is required!");
xPhone.requestFocus();
return;
}
if(xMobileNumber.length()<10){
xPhone.setError("Please enter a valid phone number!");
xPhone.requestFocus();
return;
}
PhoneAuthProvider.getInstance().verifyPhoneNumber(
xMobileNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
(Executor) this, // Activity (for callback binding) //NOT SURE ABOUT THIS
mCallbacks); // OnVerificationStateChangedCallbacks
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks=new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
codeSent=s;
}
};
}
LOGCAT:2020-06-10 03:59:44.148 16491-16491/com.example.XX
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.XX, PID: 16491
java.lang.ClassCastException: com.example.XX.SignInFragment cannot be cast to java.util.concurrent.Executor
at com.example.XX.SignInFragment.sendVerificationCode(SignInFragment.java:132)
at com.example.XX.SignInFragment.access$000(SignInFragment.java:29)
at com.example.XX.SignInFragment$1.onClick(SignInFragment.java:65)
at android.view.View.performClick(View.java:7201)
at android.view.View.performClickInternal(View.java:7170)
at android.view.View.access$3500(View.java:806)
at android.view.View$PerformClick.run(View.java:27582)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7695)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
活动中使用的此
。在片段中使用getActivity
而不是this
替换
PhoneAuthProvider.getInstance().verifyPhoneNumber(
xMobileNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
(Executor) this, // Activity (for callback binding) //NOT SURE ABOUT THIS
mCallbacks); // OnVerificationStateChangedCallbacks
}
与
PhoneAuthProvider.getInstance().verifyPhoneNumber(
xMobileNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
getActivity(), // Activity (for callback binding) //NOT SURE ABOUT THIS
mCallbacks); // OnVerificationStateChangedCallbacks
}
if (TextUtils.isEmpty(codeSent) || codeSent == null) {
Toast.makeText(this, "Please wait until code received", Toast.LENGTH_SHORT).show();
return;
}
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, tOTP);
signInWithPhoneAuthCredential(credential);
我一直在尝试使用Firebase认证来实现电话认证。它和一个Android模拟器一起工作。我在模拟器中使用了一个测试号和一个实数。两个作品。但是只有测试号可以在真实设备上工作,并对其他号抛出这个异常 我也在Firebase中设置了SHA密钥。
我已经在我的android应用程序中添加了Firebase手机认证,它运行良好,但是应用程序名称没有包含在短信验证消息中,因为它出现在Firebase控制台的短信模板中:(%LOGIN_CODE%是%APP_NAME%的校验码。)。 我收到的消息如下:(%LOGIN_CODE%是您的验证码) 我的应用程序的调试版本和发布版本都有这个问题。 那么,如何将应用程序名称添加到此消息中。
嗨,我正在尝试做一个手机身份验证在android使用Firebase。第一次我安装应用程序,短信来和验证是成功的,但随后短信没有再来。我已经从firebase的身份验证中删除了该用户,但它仍然不起作用。 下面是我的代码。
我已经在我现有的android项目上附加了firebase,并运行了它。这是一个场景:如果应用程序通过开发者模式从android studio运行,它(通过电话号码登录Firebase)运行平稳,我也能成功登录。但是,每当我构建签名的调试apk,Firebase都不允许登录。它给出了以下信息: 此应用程序未被授权使用Firebase身份验证。请验证firebase控制台中配置了正确的包名称和SHA
我一直在尝试使用Firebase Auth执行电话身份验证方法,我已确保遵循所有步骤,添加我的包名称,使用添加SHA Key keytool -list -v -keystore “%USERPROFILE%.android\debug.keystore” -alias androiddebugkey -storepass android -keypass android 然后添加所有的依赖项,并
我已经正确地实现了Firebase电子邮件和Google登录,它在iOS和Android上都运行良好。然后我试着实现通过电话的身份验证,首先它在iPhone上不起作用,但后来我做了一些研究,似乎我必须上传一些APN,最后我正确地设置了它,现在它在iPhone上起作用了,但当我尝试向Android发送短信时,它没有接收到它,而是发送给我一条消息,表明代码是在iPhone上发送的,就像在iPhone上