Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MySMSBroadcastReceiver : BroadcastReceiver() {
- private var otpReceiver: OTPReceiveListener? = null
- /**
- * @param receiver OTPReceiveListener
- */
- fun initOTPListener(receiver: OTPReceiveListener) {
- this.otpReceiver = receiver
- }
- override fun onReceive(context: Context, intent: Intent) {
- if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
- val extras : Bundle = intent.extras
- val status : Status? = extras.get(SmsRetriever.EXTRA_STATUS) as? Status
- when (status?.statusCode) {
- CommonStatusCodes.SUCCESS -> {
- // Get SMS message contents
- val otp : String? = extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as? String
- // 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
- otpReceiver?.let { otpReceiveListener ->
- otp?.let {otp ->
- otpReceiveListener.onOTPReceived(otp.replace("<#> Your ExampleApp code is: ", "").split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0])
- }
- }
- }
- CommonStatusCodes.TIMEOUT ->
- // Waiting for SMS timed out (5 minutes)
- // Handle the error ...
- otpReceiver?.onOTPTimeOut()
- }
- }
- }
- /**
- * Listener for Received OTP
- */
- interface OTPReceiveListener {
- /**
- * @param otp String
- */
- fun onOTPReceived(otp: String)
- /**
- * Time Out
- */
- fun onOTPTimeOut()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement