Issue
Not sure what I'm doing wrong, I've searched on Stack Overflow and read articles and watched videos but still no solution.
Basically I want to send a SMS message by Firebase to user's phone to verify the phone number. My app is not in the Play Store or anything like that, I'm just running it by connecting my phone to PC with a USB cable.
As far as I know I have set it up correctly. This is what I see in Android Studio:
Here is my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText editTextPhone, editTextCode;
FirebaseAuth mAuth;
String codeSent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
editTextCode = findViewById(R.id.editTextCode);
editTextPhone = findViewById(R.id.editTextPhone);
findViewById(R.id.buttonGetVerificationCode).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendVerificationCode();
}
});
findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
verifySignInCode();
}
});
}
private void verifySignInCode(){
String code = editTextCode.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//here you can open new activity
Toast.makeText(getApplicationContext(),
"Login Successfull", Toast.LENGTH_LONG).show();
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(getApplicationContext(),
"Incorrect Verification Code ", Toast.LENGTH_LONG).show();
}
}
}
});
}
private void sendVerificationCode(){
String phone = editTextPhone.getText().toString();
if(phone.isEmpty()){
editTextPhone.setError("Phone number is required");
editTextPhone.requestFocus();
return;
}
if(phone.length() < 10 ){
editTextPhone.setError("Please enter a valid phone");
editTextPhone.requestFocus();
return;
}
System.out.println("code should be sent");
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phone, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(FirebaseException e) {
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
codeSent = s;
}
};
}
When I click the Get Verification Button I see the System.out.println
in the log, "code should be sent" so button is working ok, just no code gets sent. Any ideas please?
Also, I believe I have set up my SHA-1 correctly. I got it from Android Studio by clicking "signingReport" from the Gradle tab and copying the number into Firebase Console, as below.
Solution
My code was based on this video and I copied and pasted source code from the github account recommended:
https://www.youtube.com/watch?v=N6HBStmDkMQ
Everything looked good in my Firebase console, and the sourcecode was loading and working with no errors - working apart from the SMS being sent I mean - so I copied and pasted the code from this project instead, without needing to change any FireBase settings:
https://www.simplifiedcoding.net/firebase-phone-authentication-android-tutorial/
Now it is sending SMS messages.
Answered By - CHarris