Я не могу отправить телефонный логин смс код через firebase и я не знаю почему - PullRequest
0 голосов
/ 18 марта 2020

Опция PhoneLogin включена в методах аутентификации Firebase. Я попытался переустановить приложение, выполняя другой проект и заново подключая все что угодно с помощью firebase, но ничего не вышло.

Ниже вы можете увидеть мой код активности, а под ним я также разместил свой код макета, надеюсь, что это поможет.


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
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.Objects;
import java.util.concurrent.TimeUnit;

public class PhoneLoginActivity extends AppCompatActivity {

    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    private PhoneAuthProvider.ForceResendingToken mResendToken;
    private String verificareID;
    private EditText phoneNumber,phoneNumberCode;
    private Button getPhoneCode,verifyPhoneCode,numarGresit;
    private FirebaseAuth mAuth;
    private ProgressDialog loadingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_login);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        initializeVariables();
        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
                SignInWithPhoneCredentials(phoneAuthCredential);
            }

            @Override
            public void onVerificationFailed(@NonNull FirebaseException e) {
                getPhoneCode.setVisibility(View.VISIBLE);
                phoneNumber.setVisibility(View.VISIBLE);
            }
            @Override
            public void onCodeSent(@NonNull String verificationId,
                                   @NonNull PhoneAuthProvider.ForceResendingToken token) {
                verificareID = verificationId;
                mResendToken = token;
                loadingBar.dismiss();
                getPhoneCode.setVisibility(View.GONE);
                phoneNumber.setText("");
                phoneNumber.setVisibility(View.GONE);
            }
        };
        getPhoneCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String numarTelefon = phoneNumber.getText().toString();
                if(TextUtils.isEmpty(numarTelefon)){
                    Toast.makeText(PhoneLoginActivity.this, "Introduceti un numar de telefon", Toast.LENGTH_LONG).show();
                } else {
                    loadingBar.setTitle("Verificare numar");
                    loadingBar.setMessage("Va rog asteptati..");
                    loadingBar.setCanceledOnTouchOutside(false);
                    loadingBar.show();
                    numarGresit.setVisibility(View.VISIBLE);
                    PhoneAuthProvider.getInstance().verifyPhoneNumber(
                            numarTelefon,
                            60,
                            TimeUnit.SECONDS,
                            PhoneLoginActivity.this,
                            mCallbacks);
                }
            }
        });

        verifyPhoneCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String codVerificare = phoneNumberCode.getText().toString();
                if(TextUtils.isEmpty(codVerificare)){
                    Toast.makeText(PhoneLoginActivity.this, "Introduceti un cod de verificare", Toast.LENGTH_LONG).show();
                } else {
                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificareID,codVerificare);
                    SignInWithPhoneCredentials(credential);
                }
            }
        });
        numarGresit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPhoneCode.setVisibility(View.VISIBLE);
                phoneNumber.setText("");
                phoneNumber.setVisibility(View.VISIBLE);
                numarGresit.setVisibility(View.GONE);
            }
        });

    }

    private void SignInWithPhoneCredentials(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            loadingBar.dismiss();
                            Toast.makeText(PhoneLoginActivity.this, "Logare cu succes!", Toast.LENGTH_LONG).show();
                            SendUserToMainActivity();
                        } else {
                            Toast.makeText(PhoneLoginActivity.this, "Error", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void initializeVariables(){
        phoneNumber = findViewById(R.id.phoneNumber);
        phoneNumberCode = findViewById(R.id.codePhone);
        getPhoneCode = findViewById(R.id.getVerificationCode);
        verifyPhoneCode = findViewById(R.id.verifyCode);
        mAuth = FirebaseAuth.getInstance();
        loadingBar = new ProgressDialog(this);
        numarGresit = findViewById(R.id.wrongPhoneNumber);
    }
    private void SendUserToMainActivity(){
        Intent sendUserToMainActivity = new Intent(getBaseContext(),MainActivity.class);
        startActivity(sendUserToMainActivity);
        finish();
    }
    private void SendUserToLoginActivity(){
        Intent intent = new Intent(getBaseContext(), LoginActivity.class);
        startActivity(intent);
        finish();
    }
}

А вот макет, если он поможет ..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
    android:id="@+id/toolbar"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Toolbar Title"
                    android:layout_gravity="center"
                    android:id="@+id/toolbar_title" />

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="310dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentEnd="true"
                android:layout_centerHorizontal="true"
                android:layout_marginStart="0dp"
                android:layout_marginTop="0dp"
                android:layout_marginBottom="0dp"
                android:scaleType="fitXY"
                android:src="@drawable/hospitalphonelogin"/>

            <EditText
                android:id="@+id/phoneNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/imageView"
                android:layout_marginTop="30dp"
                android:ems="10"
                android:hint="@string/numarul_de_telefon"
                android:inputType="phone"
                />

            <EditText
                android:id="@+id/codePhone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/phoneNumber"
                android:autofillHints="Parola"
                android:ems="10"
                android:hint="@string/codul_de_verificare"
                android:inputType="number"
                tools:ignore="TextFields" />

            <Button
                android:id="@+id/getVerificationCode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/codePhone"
                android:layout_marginLeft="100dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="100dp"
                android:text="@string/trimite_codul" />

            <Button
                android:id="@+id/verifyCode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/getVerificationCode"
                android:layout_marginLeft="100dp"
                android:layout_marginRight="100dp"
                android:text="@string/verificare_cod" />

            <Button
                android:id="@+id/wrongPhoneNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/verifyCode"
                android:layout_marginLeft="100dp"
                android:layout_marginRight="100dp"
                android:text="@string/numar_gresit"
                android:visibility="gone" />
            <CheckBox
                android:id="@+id/checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/doctor"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/wrongPhoneNumber"
                />

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...