广播接收器未从 Google 短信检索器 API 接收短信
Broadcast Receiver is not receiving SMS from Google SMS retriever API
我正在尝试从 google 短信检索器 API 中自动获取 OTP,但我的广播接收器中没有收到任何 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>
广播接收器class:
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()
}
}
}
}
Activity:
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 中的日志,但不在广播接收器中。请帮帮我。
我正在牛轧糖上试用这个程序,但希望所有版本都兼容。
根据 documentation, the SMS must include your app's hash, which is generated from your app's package name and public cert used to sign it. The hash generation is described here,该站点的摘要:
The following command computes the hash string from your app's production keystore:
keytool -exportcert -alias PlayDeploymentCert -keystore MyProductionKeys.keystore | xxd -p | tr -d "[:space:]" | echo -n com.example.myapp cat
| sha256sum | tr -d "[:space:]-" | xxd -r -p | base64 | cut -c1-11
所以 SMS 消息应该是这样的:123456 是您应用程序的 OTP FA+9qCX9VSu
我正在尝试从 google 短信检索器 API 中自动获取 OTP,但我的广播接收器中没有收到任何 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>
广播接收器class:
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()
}
}
}
}
Activity:
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 中的日志,但不在广播接收器中。请帮帮我。 我正在牛轧糖上试用这个程序,但希望所有版本都兼容。
根据 documentation, the SMS must include your app's hash, which is generated from your app's package name and public cert used to sign it. The hash generation is described here,该站点的摘要:
The following command computes the hash string from your app's production keystore:
keytool -exportcert -alias PlayDeploymentCert -keystore MyProductionKeys.keystore | xxd -p | tr -d "[:space:]" | echo -n com.example.myapp
cat
| sha256sum | tr -d "[:space:]-" | xxd -r -p | base64 | cut -c1-11
所以 SMS 消息应该是这样的:123456 是您应用程序的 OTP FA+9qCX9VSu