1

I am trying to Read Sms by using this method. But my application is not reading Message.

The Code i have tried yet.

Permission :

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

Activity (Main Code) :

class OtpActivity : AppCompatActivity(), View.OnClickListener {
    private var smsVerifyCatcher: SmsVerifyCatcher? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_otp)
        smsVerifyCatcher = SmsVerifyCatcher(this, OnSmsCatchListener { message ->
            val code = parseCode(message)//Parse verification code
            Log.e("Code", code)
            //then you can send verification code to server
        })
        smsVerifyCatcher!!.setPhoneNumberFilter("0902249") // I passed 10 digit number here
        smsVerifyCatcher!!.setFilter("Ashish") // For extra i added Filter for name
    }
    private fun parseCode(message: String): String {
        val p = Pattern.compile("\\b\\d{4}\\b")
        val m = p.matcher(message)
        var code = ""
        while (m.find()) {
            code = m.group(0)
        }
        return code
    }
    override fun onStart() {
        super.onStart()
        smsVerifyCatcher!!.onStart()
    }

    override fun onStop() {
        super.onStop()
        smsVerifyCatcher!!.onStop()
    }
}
Steve Vinoski
  • 18,969
  • 3
  • 26
  • 38
Ashish
  • 6,005
  • 3
  • 17
  • 40

1 Answers1

6

It's not a good idea because of this Reminder SMS/Call Log Policy Changes.

The recomended way is using SMS Retriever API from Google Play Services. See the Automatic SMS Verification with the SMS Retriever API.

Notice though that your server needs to send the messages following a few rules (message starts with "<#>", includes the OTP plus additional information and ends up with a hash identifying your app).

Xavier Rubio Jansana
  • 5,998
  • 1
  • 23
  • 46
  • I am gonna use my private api to send msg and I just want to get text from sms. – Ashish May 06 '19 at 13:14
  • The SMS Retriever API doesn't force you to use Google's servers to send the SMS. You can still use yours (or any 3rd party service you're using). You just need to format the SMS following a few rules, and the SMS Retriever API will the message reach your app without having to get the SMS permissions (which are now not allowed except for very specific cases (as you can read in the first link of my answer). – Xavier Rubio Jansana May 06 '19 at 14:48