Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase身份验证未接收SDK高于11.8.0的短信_Android_Firebase_Firebase Authentication - Fatal编程技术网

Android Firebase身份验证未接收SDK高于11.8.0的短信

Android Firebase身份验证未接收SDK高于11.8.0的短信,android,firebase,firebase-authentication,Android,Firebase,Firebase Authentication,我正在实施Android应用程序,使用Firebase对我的用户进行身份验证 我的问题是,将Gradle依赖项从更改为: implementation 'com.google.firebase:firebase-auth:11.8.0' 如果是更高级别,我的代码将停止发送SMS身份验证消息 我的代码: public class AuthenticationActivity extends AppCompatActivity { private PhoneAuthProvider.OnV

我正在实施Android应用程序,使用Firebase对我的用户进行身份验证

我的问题是,将Gradle依赖项从更改为:

implementation 'com.google.firebase:firebase-auth:11.8.0'
如果是更高级别,我的代码将停止发送SMS身份验证消息

我的代码:

public class AuthenticationActivity extends AppCompatActivity {
    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    private EditText phoneNum;
    private PinView verifyCodeET;
    private String mVerificationId;
    private FirebaseAuth mAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authentication);
        mAuth = FirebaseAuth.getInstance();
        Button sendCodeButton = findViewById(R.id.submit1);
        Button verifyCodeButton = findViewById(R.id.submit2);
        phoneNum = findViewById(R.id.phonenumber);
        verifyCodeET = findViewById(R.id.pinView);
        sendCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phoneNumber = phoneNum.getText().toString();
                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        phoneNumber, // Phone number to verify
                        60, // Timeout duration
                        TimeUnit.SECONDS, // Unit of timeout
                        AuthenticationActivity.this, // Activity (for callback binding)
                        mCallbacks); // OnVerificationStateChangedCallbacks
            }
        });
        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                signInWithPhoneAuthCredential(phoneAuthCredential);
            }
            @Override
            public void onVerificationFailed(FirebaseException e) {
            }
            @Override
            public void onCodeSent(String verificationId,PhoneAuthProvider.ForceResendingToken token) {
                mVerificationId = verificationId;
            }
        };
        verifyCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String verificationCode = verifyCodeET.getText().toString();
                boolean success = true;
                PhoneAuthCredential credential = null;
                try {
                    credential = PhoneAuthProvider.getCredential(mVerificationId, verificationCode);
                } catch (Exception e) {
                    // "Something went wrong"
                    success = false;
                }
                if (success) {
                    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()) {
                            // Sign in success
                        } else {
                            // "Invalid verification code"
                        }
                    }
                });
    }
}
Java应用程序包括:

import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
并将onClicK()函数编辑为:

@Override
public void onClick(View view) {
    String phoneNumber = phoneNum.getText().toString();
    // Check phoneNumber.length()
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    try {
        PhoneNumber numberProto = phoneUtil.parse(phoneNumber, "US");
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneUtil.format(numberProto, PhoneNumberUtil.PhoneNumberFormat.E164), // Phone number to verify
                60, // Timeout duration
                TimeUnit.SECONDS, // Unit of timeout
                AuthenticationActivity.this, // Activity (for callback binding)
                mCallbacks); // OnVerificationStateChangedCallbacks
    } catch (NumberParseException e) {
        // Wrong format
    }
}

现在一切都可以使用最新的SDK版本了

试试
e.printStackTrace()
检查您遇到的错误。使用以下代码:

 mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                signInWithPhoneAuthCredential(phoneAuthCredential);
            }
            @Override
            public void onVerificationFailed(FirebaseException e) {
              e.printStackTrace();
            }
            @Override
            public void onCodeSent(String verificationId,PhoneAuthProvider.ForceResendingToken token) {
                mVerificationId = verificationId;
            }
        };
希望这对你有用

 mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                signInWithPhoneAuthCredential(phoneAuthCredential);
            }
            @Override
            public void onVerificationFailed(FirebaseException e) {
              e.printStackTrace();
            }
            @Override
            public void onCodeSent(String verificationId,PhoneAuthProvider.ForceResendingToken token) {
                mVerificationId = verificationId;
            }
        };