问题描述
尝试按照以下步骤实施SMS自动读取以进行验证: https://developers.google.com/identity/sms-retriever/request
Tried to implement SMS auto-reading for verification following the steps here:https://developers.google.com/identity/sms-retriever/request
1)声明
lateinit var smsRetrieverClient: SmsRetrieverClient
private lateinit var smsReceiver: SmsBrReceiver
2)在登录活动onCreate上初始化并注册它们
2) Initialised and registered them in login Activity onCreate
smsRetrieverClient = SmsRetriever.getClient(this)
smsReceiver = SmsBrReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION)
applicationContext.registerReceiver(smsReceiver, intentFilter)
val task = smsRetrieverClient.startSmsRetriever()
task.addOnSuccessListener(OnSuccessListener<Void> {
smsReceiver.setTimeout()
})
task.addOnFailureListener(OnFailureListener { e ->
showCodeInput()
})
3)制作了用于SmsRetriever的BroadcastReceiver
3) Made BroadcastReceiver for SmsRetriever
inner class SmsBrReceiver : BroadcastReceiver() {
var h = Handler()
var r: Runnable = Runnable { doTimeout() }
fun setTimeout() {
h.postDelayed(r, 600000)
}
override fun onReceive(context: Context, intent: Intent?) {
val action = intent.action
if (SmsRetriever.SMS_RETRIEVED_ACTION == action) {
cancelTimeout()
val extras = intent.extras
val status = extras!!.get(SmsRetriever.EXTRA_STATUS) as Status
when (status.statusCode) {
CommonStatusCodes.SUCCESS -> { // not called
4)短信格式为
\u200b\u200bPlease enter code: 1111 /appKeyXf56
SMS末尾的应用程序密钥与已安装的应用程序签名密钥生成的密钥相匹配,如下所述: https://developers.google.com/identity/sms-retriever/verify# computing_your_apps_hash_string
And the app key at the end of SMS matches what the installed app signing key gives for generation as described here:https://developers.google.com/identity/sms-retriever/verify#computing_your_apps_hash_string
CommonStatusCodes.SUCCESS在收到SMS后不会被调用,但是CommonStatusCodes.TIMEOUT会在5分钟后被调用.
CommonStatusCodes.SUCCESS does not get called after SMS is received, but CommonStatusCodes.TIMEOUT does get called after 5 minutes.
缺少什么?万一电话认证的gradle声明是:
What is missing? Just in case gradle declarations for phone auth are:
com.google.android.gms:play-services-auth-api-phone:16.0.0
classpath "com.google.gms:google-services:4.2.0
推荐答案
解决方案是缩短消息,尽管它似乎不超过140个字节.将SMS文本更改为大约30个字符后,无需其他更改即可自动读取SMS.
The solution was to shorten the message, although it seemed to not be over 140 bytes. After making the SMS text to ~30 characters auto-read of SMS works without other changes.
这篇关于使用Android SmsRetrieverClient进行SMS验证未解析消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!