Google SMS Retriever API not retreiving SMS messages

戏子无情 提交于 2019-12-12 12:14:50

问题


I'm trying to use Google's SMS Retriever API for Automatic SMS Verification. I have followed the directions here but my app is not receiving any SMS messages. Here is what I've done:

I've added the code in my activity to start the SMS retriever client:

    val client = SmsRetriever.getClient(this)
    val retriever = client.startSmsRetriever()
    retriever.addOnSuccessListener {
        Log.i("loginActivity", "started smsretriever")
    }

    retriever.addOnFailureListener {
        //Problem to start listener
    }

From the logs, I see this starts successfully. Then I add my broadcast receiver with this onReceive:

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

        when (status.statusCode) {
            CommonStatusCodes.SUCCESS -> {

            }
            CommonStatusCodes.TIMEOUT -> {

            }
    }
}

This only triggers when a TIMEOUT event is sent 5 minutes after the SMS retriever client starts. It never triggers for SMS messages.

Finally, I've registered the receiver in the Manifest:

<receiver android:name=".receiver.SMSReceiver" android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
        </intent-filter>
    </receiver>

In terms of the text message, I am sending myself this SMS message: "<#> Your code is: 123456 verification hash: "

Any idea what I'm doing wrong?


回答1:


Maybe your message has wrong construction, please check this link: SMS Retriever API. At the first point, your text message must be begin with <#> or [#]. And at the 4th point, Your text message must be-"End with an 11-character hash string that identifies your app"

I have the same issue and that points is my awareness, solve by adding <#> in begining and hash string at the end.




回答2:


I did the same way and it was perfectly working for me. Most likely that your application hash is wrong.




回答3:


I faced the same problem recently.

I figured out the problem was that I was using the production Keystore hash for testing in debug mode.

If you're running in debug mode, make sure to use the default keystore used to sign the debug apk.

The default debug keystore location is $HOME/.android/debug.keystore

As per the app-sign documentation:

When running or debugging your project from the IDE, Android Studio automatically signs your app with a debug certificate generated by the Android SDK tools. The first time you run or debug your project in Android Studio, the IDE automatically creates the debug keystore and certificate in $HOME/.android/debug.keystore, and sets the keystore and key passwords.

I recently wrote a blog post about this, check it out for more details.




回答4:


Register the BroadcastReceiver inside SmsRetrievers addOnSuccessListener callback method, don't register in Manifest file.

val client = SmsRetriever.getClient(this)
        val retriever = client.startSmsRetriever()
        retriever.addOnSuccessListener {
            val listener = object : SMSBroadcastReceiver.Listener {
                override fun onSMSReceived(otp: String) {
                    Log.d("AppSignatureHelper",otp)
                    Toast.makeText(context, otp, Toast.LENGTH_SHORT).show()
                }

                override fun onTimeOut() {
                    Log.d("AppSignatureHelper","Timed Out.")
                }
            }
            smsBroadcastReceiver.injectListener(listener)
            registerReceiver(smsBroadcastReceiver, IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION))
        }
        retriever.addOnFailureListener {
            Log.d("AppSignatureHelper","Problem to start listener")
            //Problem to start listener
        }

Listener Interface is.

 interface Listener {
        fun onSMSReceived(otp: String)
        fun onTimeOut()
    }



回答5:


What have you done as far now is perfect, just small thing add the below in your activity also, it worked for me...........

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION);

getApplicationContext().registerReceiver(smsBroadcast, intentFilter);



来源:https://stackoverflow.com/questions/53908267/google-sms-retriever-api-not-retreiving-sms-messages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!