public class MainActivity extends AppCompatActivity {
public String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText phoneNumber = (EditText) findViewById(R.id.editTextPhone);
phoneNumber.setText(getPhone());
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
String TAG = "FCM";
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
String token = task.getResult().getToken();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference(getPhone());
myRef.setValue(token);
}
});
}
public String getPhone() {
TelephonyManager phoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
0);
}
String _phone_number = phoneMgr.getLine1Number();
return _phone_number != null && _phone_number.length() > 2 ? _phone_number.substring(2) : null;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if(requestCode==0) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getPhone();
} else {
Toast.makeText(MainActivity.this,
"Permission Not Granted",
Toast.LENGTH_SHORT).show();
}
}
}
public void loginUser(View view){
EditText _phone_view = (EditText)findViewById(R.id.editTextPhone);
EditText _password_view = (EditText)findViewById(R.id.editTextPassword);
String _phone = _phone_view.getText().toString();
String _passwd = _password_view.getText().toString();
String message = "Username: " + _phone + "\nPassword: " + _passwd;
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
sendVerificationCodeToUser("+91" + getPhone());
setContentView(R.layout.activity_home);
}
private void sendVerificationCodeToUser(String _phone) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
_phone,
60,
TimeUnit.SECONDS,
TaskExecutors.MAIN_THREAD,
mCallbacks);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
//Getting the code sent by SMS
Toast.makeText(getApplicationContext(),"Verifying code Automatically",Toast.LENGTH_LONG).show();
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
mVerificationId = s;
mResendToken = forceResendingToken;
}
};
private void verifyVerificationCode(String otp) {
//creating the credential
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, otp);
//signing the user
signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//verification successful we will start the profile activity
Toast.makeText(getApplicationContext(),
"Verified",
Toast.LENGTH_SHORT).show();
Button button = (Button) findViewById(R.id.button3);
button.setText("Logged In");
} else {
//verification unsuccessful.. display an error message
String message = "Somthing is wrong, we will fix it soon...";
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
message = "Invalid code entered...";
}
}
}
});
}
}
我的日志记录中出现此错误
2020-10-06 17:59:28.523 9772-28114 /?电子/凌空:[10545] BasicNetwork.performRequest:的意外响应代码503 https://www.googleapis.com/identitytoolkit/v3/relyingparty/sendVerificationCode?alt=proto&key=AIzaSyCzVM68ITaGzh-r7CdTor2CBEAZY2Hg45A
我没有收到任何OTP,该吐司消息是从onVerificationFailed生成的:Internal Error: Error Code 39