Sharath Prakash
Sharath Prakash

Reputation: 326

Firebase PhoneAuthProvider OTP

I have made a simple chat app and I am trying to add the phone number verification using Firebases Phone Authentication. I have successfully added all dependencies and the database works fine. But I am not getting any code via message when I hit the send OTP button.

Here is the full class.

package com.sharathnewdev.flashchatnewfirebase;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;

public class FB_Phone extends AppCompatActivity {
    FirebaseAuth mAuth;
    EditText et1, et2;
    Button sendbutton, verifybutton;
    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    String Verificationcode;
    private static final String TAG = "FB_Phone";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fb_phone);
        et1 = findViewById(R.id.mobileNUMBERTV);
        et2 = findViewById(R.id.verifyTV);
        sendbutton = findViewById(R.id.sendbutton);
        verifybutton = findViewById(R.id.verifybutton);
        sendbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_Sms();
            }
        });
        verifybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify();
            }
        });
        mAuth = FirebaseAuth.getInstance();
        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);
                Verificationcode = s;
                Toast.makeText(getApplicationContext(), Verificationcode, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "The code has been sent to the user", Toast.LENGTH_SHORT).show();
            }
        };

    }

    public void send_Sms() {
        String sent_code = et1.getText().toString();
        Toast.makeText(getApplicationContext(), sent_code, Toast.LENGTH_SHORT).show();
        PhoneAuthProvider.getInstance().verifyPhoneNumber(sent_code, 60, TimeUnit.SECONDS, this, mCallbacks);
    }

    public void signInWithPhone(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(getApplicationContext(), "The use has been successfully signed in", Toast.LENGTH_SHORT).show();
                    Intent chatIntent = new Intent(FB_Phone.this, MainChatActivity.class);
                    startActivity(chatIntent);
                }
            }
        });
    }

    public void verify() {
        String recieved_code = et2.getText().toString();
        VerifywithPhone(Verificationcode, recieved_code);

    }

    public void VerifywithPhone(String verificationcode, String recievedcode) {
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationcode, recievedcode);
        signInWithPhone(credential);

    }
}

In this, I am calling the send_sms method when the send button is pressed. But it just doesn't send me anything via SMS. I tried debugging it but the process just runs and nothing happens

Logcat shows this error

java.lang.IllegalArgumentException: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, ortemprary proof.

Please help. What am I doing wrong here.?

Upvotes: 2

Views: 1167

Answers (2)

Sharath Prakash
Sharath Prakash

Reputation: 326

Thanks everyone for the responses. I eventually figured out the problem.

It was that I hadn't added my apps SHA-1 fingerprint to the firebase console.

Also until today, I was not checking the onVerificationFailed method for any potential errors.

I added the code to show a toast message on the OnverificationFailed method.

I got the error clearly and then added the SHA-1 fingerprint to console using this guide.

It was silly of me to not use it.

Thanks again.

Upvotes: 1

Stormy Chase Forrester
Stormy Chase Forrester

Reputation: 313

It is so simple first start the emulator and launches your application. now if you have another project(any) launch this in another emulator. On the other emulator start SMS application and send sms to the emulator in which your app is running.The phone no. is simply the emulator number like 5556 or 5554.

Edit Now You can send an SMS or make a call using Emulator Control. In Eclipse go to window->show views->other->Emulator control.

https://stackoverflow.com/a/7638361/3980203

Upvotes: 0

Related Questions