Created
May 10, 2019 19:17
-
-
Save MehulKK/684b417d1fa72a9b9414ae01fa18c97a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment