我想在我的应用程序中实现这个用于otp验证的api,请求用户同意从服务器(firebase)发送的文本消息中读取验证代码,并且我遵循了googleapi指南中的所有指导原则(https://developers.google.com/identity/sms-retriever/user-consent/request). 这是文件
otpverification活动是我想要实现这个功能的地方。当otp从服务器成功发送并且用户从登录活动进入otp验证活动时,应用程序崩溃。
下面是调试屏幕的截图
这是我的logcat截图,应用程序崩溃了
OTP验证活动代码
class OTPVerification : AppCompatActivity() {
private val SMS_CONSENT_REQUEST = 2
private val TAG = "OTPVerification"
private lateinit var otpPinView: PinView
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_o_t_p_verification)
otpPinView = findViewById(R.id.otp_pin_view)
val otpSentToTextView = findViewById<TextView>(R.id.otp_sent_to_tv)
val toolbar = findViewById<Toolbar>(R.id.otp_verification_toolbar)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
SmsRetriever.getClient(this).startSmsUserConsent(57575454.toString())
val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
registerReceiver(smsVerificationReceiver, intentFilter, 0)
val intent = intent
val userFullMobileNumber = intent.getStringExtra("userMobileNo")
otpSentToTextView.text = resources.getString(R.string.otp_sent_to, userFullMobileNumber)
}
private val smsVerificationReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
val extras = intent.extras
val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status
when (smsRetrieverStatus.statusCode) {
CommonStatusCodes.SUCCESS -> {
// Get consent intent
val consentIntent : Intent? =
extras.getParcelable(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) {
Log.d(TAG, "onReceive: ${e.message}")
}
}
CommonStatusCodes.TIMEOUT -> {
Toast.makeText(baseContext, "Session timed out", Toast.LENGTH_SHORT).show()
}
}
}
}
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
SMS_CONSENT_REQUEST ->
// Obtain the phone number from the result
if (resultCode == Activity.RESULT_OK && data != null) {
// Get SMS message content
val message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)
// Extract one-time code from the message and complete verification
// `message` contains the entire text of the SMS message, so you will need
// to parse the string.
val oneTimeCode = parseOneTimeCode(message)
otpPinView.setText(oneTimeCode)
} else {
Toast.makeText(baseContext, "Unable to detect OTP. Please enter OTP manually.", Toast.LENGTH_SHORT).show()
}
}
}
private fun parseOneTimeCode(message: String?): Int {
return message!!.substring(0..5).toInt()
}
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(smsVerificationReceiver)
}
}
谢谢。
暂无答案!
目前还没有任何答案,快来回答吧!