#android #api #mobile #one-time-password
Вопрос:
Я пытаюсь реализовать API проверки SMS с одним касанием.
Я запускаю повторный просмотр sms в CreateView
private val SMS_CONSENT_REQUEST = 2
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_otp, container, false)
registrationActivity().component.inject(this)
val task = SmsRetriever.getClient(registrationActivity()).startSmsUserConsent(null)
task.addOnSuccessListener {
Log.d(TAG, "SMS retriever successfully started")
}
task.addOnFailureListener {
Log.e(TAG, "SMS retriever failed to start ${it.message}")
}
return rootView
}
это широковещательный приемник, который должен запустить представление согласия
private val smsVerificationReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val extras = intent.extras
val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status
when (smsRetrieverStatus.statusCode) {
CommonStatusCodes.SUCCESS -> {
// Get consent intent
val consentIntent = extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)
try {
// Start activity to show consent dialog to user, activity must be started in
// 5 minutes, otherwise you'll receive another TIMEOUT intent
startActivityForResult(consentIntent, SMS_CONSENT_REQUEST)
} catch (e: ActivityNotFoundException) {
// Handle the exception ...
Log.e(TAG,"something happened ${e.message}")
}
}
CommonStatusCodes.TIMEOUT -> {
Log.d(TAG, "timeout")
// Time out occurred, handle the error.
}
}
}
}
и я регистрирую приемник вещания в onResume
override fun onResume() {
super.onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
activity!!.registerReceiver(smsVerificationReceiver, intentFilter, SmsRetriever.SEND_PERMISSION,null)
Log.d(TAG, "registered the sms verification receiver")
}
}
Я получаю SMS от неконтактного пользователя с текстом «Ваш код 221-222», но он не принимается. Есть какие-нибудь мысли? Спасибо.