Android 没有';无法从firebase phone auth获取sms OTP代码
请帮助我,我正在尝试使用firebase制作一个电话认证应用程序。我已激活电话登录方法 这是我的应用程序gradleAndroid 没有';无法从firebase phone auth获取sms OTP代码,android,firebase,firebase-authentication,one-time-password,Android,Firebase,Firebase Authentication,One Time Password,请帮助我,我正在尝试使用firebase制作一个电话认证应用程序。我已激活电话登录方法 这是我的应用程序gradle apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'com.google.gms.google-services' android { compileSdkVersio
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.lumbung_inovasi.policehealthcare"
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "1.0"
buildConfigField("String", "BASE_API", '"http://ludes.in:5001/"')
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// To inline the bytecode built with JVM target 1.8 into
// bytecode that is being built with JVM target 1.6. (e.g. navArgs)
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.fragment:fragment:1.2.5"
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-core:17.4.3'
implementation 'com.google.firebase:firebase-auth:19.3.1'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'de.hdodenhof:circleimageview:2.1.0'
implementation 'com.tbuonomo.andrui:viewpagerdotsindicator:4.1.2'
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
implementation 'com.github.alahammad:android-OTP-View:1.0.2'
// Retrofit & OkHttp
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation('com.facebook.stetho:stetho-okhttp3:1.5.1') {
exclude group: 'com.facebook.stetho'
}
}
我试图用片段创建OTP布局,并使用sms捕获库来读取带有OTP代码的新消息。这是我的OTP片段代码
class OtpFragment : Fragment(), OTPListener, OnSmsCatchListener<String> {
private lateinit var smsCatcher: SmsVerifyCatcher
private lateinit var phoneAuth: FirebaseAuth
private var mResendToken: ForceResendingToken? = null
private var mVerificationId: String = ""
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
otp.setOnOtpFinished(this)
phoneAuth = FirebaseAuth.getInstance()
smsCatcher = SmsVerifyCatcher(activity, this)
val phoneNumber = "+62 85157233868"
requestOTP(phoneNumber)
resend_button.setOnClickListener {
resendOTPCode(phoneNumber)
}
}
private fun requestOTP(phoneNumber: String){
PhoneAuthProvider.getInstance().verifyPhoneNumber(phoneNumber, 60, TimeUnit.SECONDS, this.requireActivity(), callbacks())
}
private fun callbacks(): OnVerificationStateChangedCallbacks {
return object : OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(phoneAuthCredential: PhoneAuthCredential) {
Log.e("onVerificationCompleted", "${phoneAuthCredential.smsCode}")
phoneAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener {
if (it.isSuccessful){
Log.e("sign-in", "berhasil")
} else{
Log.e("sign-in", "${it.result}")
}
}
}
override fun onVerificationFailed(e: FirebaseException) {
if (e is FirebaseAuthInvalidCredentialsException) {
Log.e("invalidCredential", e.toString())
} else if (e is FirebaseTooManyRequestsException) {
Log.e("out of quota", e.toString())
}
}
override fun onCodeAutoRetrievalTimeOut(s: String) {
super.onCodeAutoRetrievalTimeOut(s)
Log.e("", s)
}
override fun onCodeSent(
verificationId: String,
forceResendingToken: ForceResendingToken
) {
super.onCodeSent(verificationId, forceResendingToken)
Log.e("code-sent", "onCodeSent:$verificationId")
mVerificationId = verificationId
mResendToken = forceResendingToken
}
}
}
private fun signInWithCredential(credential: PhoneAuthCredential){
phoneAuth.signInWithCredential(credential)
.addOnCompleteListener {
val intent = Intent(activity, MainActivity::class.java)
startActivity(intent)
activity?.overridePendingTransition(0,0)
}
.addOnFailureListener {
Log.e("login-fail", "${it.message}")
}
}
private fun resendOTPCode(phoneNumber: String){
if(mResendToken != null) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber,
120,
TimeUnit.SECONDS,
this.requireActivity(),
callbacks(),
mResendToken
)
Toast.makeText(activity, "code otp dikirim ulang", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_otp, container, false)
}
override fun otpFinished(p0: String?) {
Toast.makeText(activity, "otp finish $p0", Toast.LENGTH_SHORT).show()
p0?.let {
val credential = PhoneAuthProvider.getCredential(mVerificationId, it)
signInWithCredential(credential)
}
}
override fun onSmsCatch(p0: String?) {
Toast.makeText(activity, "sms catch $p0", Toast.LENGTH_SHORT).show()
}
}
类OtpFragment:Fragment(),OTPListener,onmsCatchListener{
私有lateinit var smsCatcher:SmsVerifyCatcher
私有lateinit var phoneAuth:FirebaseAuth
私有变量mResendToken:ForceResendingToken?=null
private var mvericationid:String=“”
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
super.onViewCreated(视图,savedInstanceState)
otp.SETONOTP已完成(此)
phoneAuth=FirebaseAuth.getInstance()
smsCatcher=SmsVerifyCatcher(活动,此)
val phoneNumber=“+62 85157233868”
请求OTP(电话号码)
重新发送\u按钮。设置单击侦听器{
重新发送代码(电话号码)
}
}
私人娱乐请求OTP(电话号码:字符串){
PhoneAuthProvider.getInstance().verifyPhoneNumber(phoneNumber,60,TimeUnit.SECONDS,this.require(),callbacks())
}
private fun回调():OnVerificationStateChangedCallbacks{
返回对象:OnVerificationStateChangedCallbacks(){
覆盖验证完成(phoneAuthCredential:phoneAuthCredential){
Log.e(“onVerificationCompleted”,“${phoneAuthCredential.smsCode}”)
phoneAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener{
如果(it.ISSUCCESS){
Log.e(“登录”、“berhasil”)
}否则{
Log.e(“登录”,“${it.result}”)
}
}
}
覆盖验证失败(e:FirebaseException){
如果(e是FirebaseAuthInvalidCredentialsException){
Log.e(“invalidCredential”,例如toString())
}否则,如果(e是FireBaseToManyRequestsException){
Log.e(“超出配额”,例如toString())
}
}
重写onCodeAutoRetrievalTimeOut(s:字符串){
super.onCodeAutoRetrievalTimeOut(s)
Log.e(“,s)
}
迎面而来(
验证ID:字符串,
forceResendingToken:forceResendingToken
) {
super.oncodesnt(验证ID、forceResendingToken)
Log.e(“已发送代码”,“oncodesnt:$verificationId”)
mvericationId=验证ID
mResendToken=forceResendingToken
}
}
}
私人娱乐登录(凭据:PhoneAuthCredential){
phoneAuth.signInWithCredential(凭证)
.addOnCompleteListener{
val intent=intent(活动,MainActivity::class.java)
星触觉(意图)
活动?.overridePendingTransition(0,0)
}
.addOnFailureListener{
Log.e(“登录失败”,“${it.message}”)
}
}
private fun resendOTPCode(电话号码:字符串){
if(mResendToken!=null){
PhoneAuthProvider.getInstance().verifyPhoneNumber(
电话号码,
120,
时间单位:秒,
这个,
回调(),
mResendToken
)
Toast.makeText(活动,“代码otp dikirim ulang”,Toast.LENGTH\u SHORT.show())
}
}
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
}
覆盖创建视图(
充气器:布局充气器,容器:视图组?,
savedInstanceState:捆绑?
):查看{
返回充气机。充气(R.layout.fragment_otp,容器,假)
}
覆盖乐趣otpFinished(p0:字符串?){
Toast.makeText(活动,“otp完成$p0”,Toast.LENGTH\u SHORT.show()
p0?,让我来{
val credential=PhoneAuthProvider.getCredential(mvericationId,it)
使用凭据登录(凭据)
}
}
在mscatch上覆盖乐趣(p0:字符串?){
Toast.makeText(活动,“sms捕获$p0”,Toast.LENGTH\u SHORT.show())
}
}
作为参考,我使用小米redmi note 7连接互联网,sim卡处于strill活动状态