Я пытаюсь автоматически получить OTP из API Google Sms Retriever, но я не получаю OTP в моем приемнике вещания. Вот подробности:
build.gradle:
implementation "com.google.android.gms:play-services-auth-api-phone:17.3.0"
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.android.gms:play-services-base:17.0.0'
Файл манифеста:
<receiver android:name=".reciver.MySMSBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
</intent-filter>
</receiver>
Класс получателя BroadCast:
class MySMSBroadcastReceiver : BroadcastReceiver() {
private val TAG = "MySMSBroadcastReceiver"
// private var otpReceiver: OTPReceiveListener? = null
//
// fun initOTPListener(receiver: OTPReceiveListener) {
// this.otpReceiver = receiver
// }
override fun onReceive(context: Context, intent: Intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
val extras = intent.extras
val status = extras?.get(SmsRetriever.EXTRA_STATUS) as Status
Log.e("OTP_Message","hdhjvcj")
when (status.statusCode) {
CommonStatusCodes.SUCCESS -> {
// Get SMS message contents
var otp: String = extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as String
Log.e("OTP_Message", otp)
// Extract one-time code from the message and complete verification
// by sending the code back to your server for SMS authenticity.
// But here we are just passing it to MainActivity
// if (otpReceiver != null) {
// otp = otp.replace("<#> Your ExampleApp code is: ", "").split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0]
// otpReceiver!!.onOTPReceived(otp)
// }
}
CommonStatusCodes.TIMEOUT ->{
Log.e(TAG,"TimedOut")
}
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
// otpReceiver!!.onOTPTimeOut()
}
}
}
}
Активность:
val mSmsBroadcastReceiver = MySMSBroadcastReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION)
applicationContext.registerReceiver(mSmsBroadcastReceiver, intentFilter)
Ниже метод для запуска:
private fun startSMSRetrievingProcess(){
val client = SmsRetriever.getClient(this)
val task = client.startSmsRetriever()
task.addOnSuccessListener {
Log.e(TAG,"SMS received")
// Successfully started retriever, expect broadcast intent
// ...
}
task.addOnFailureListener {
Log.e(TAG,"SMS received failure")
// Failed to start retriever, inspect Exception for more details
// ...
}
}
Формат OTP: 123456 - это OTP для вашего приложения.
Я получаю журнал Внутри task.addOnSuccessListener печатается, но не внутри приемника вещания. Пожалуйста, помогите мне. Я пробую эту программу на Nougat, но хочу совместимости для всех версий.