SMS OTP Fetcher library for android
A library for implementing interception of SMS with a verification code using a few lines of code.
NB:- As per the new Playstore policy, its not allowed to use SMS Receiver or Read Permission for fetching otp.
Read more at:-
https://play.google.com/about/privacy-security-deception/permissions/
Find an alternative here:
https://developers.google.com/identity/sms-retriever/overview
Use sms fetching functionality only for Academic / Projects which won’t be published on play store.
Github link:- https://github.com/stfalcon-studio/SmsVerifyCatcher
//sms verification
implementation 'com.github.stfalcon:smsverifycatcher:0.3.2'
Requires permission for receiving and reading sms.
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
declare smsVerifyCatcher
private SmsVerifyCatcher smsVerifyCatcher;
onCreate() initialise smsVerifyCatcher
//setup sms verify for otp
smsVerifyCatcher = new SmsVerifyCatcher(this, new OnSmsCatchListener<String>() {
@Override
public void onSmsCatch(String message) {
String code = parseCode(message);//set code in edit text
if(!code.equals("")){
//TODO:- handle received code
}
//then you can send verification code to server
}
});
Codes for parsing verification code, starting and stopping catcher and for android runtime permissions. Place it after onCretae() method
/**
* Parse verification code
*
* @param message sms message
* @return only four numbers from massage string
*/
private String parseCode(String message) {
Pattern p = Pattern.compile("\\b\\d{4}\\b");
Matcher m = p.matcher(message);
String code = "";
while (m.find()) {
code = m.group(0);
}
return code;
}
@Override
protected void onStart() {
super.onStart();
smsVerifyCatcher.onStart();
}
@Override
protected void onStop() {
super.onStop();
smsVerifyCatcher.onStop();
}
/**
* need for Android 6 real time permissions
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
smsVerifyCatcher.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
Hacks for using directly from fragments
- Declare smscatcher in activity
- find view in fragment, acces by typecasting activity
- start catcher in fragment start.
- use permissions directly in activity
@Override
public void onStart() {
super.onStart();
((ActivityName)listener).smsVerifyCatcher.onStart();
}
In fragment onCreateView
//setup sms verify for otp
((LoginActivity)listener).smsVerifyCatcher = new SmsVerifyCatcher(getActivity(), new OnSmsCatchListener<String>() {
@Override
public void onSmsCatch(String message) {
String code = ((ActivityName)listener).parseCode(message);
otpView.setOTP(code);
if(!code.equals("")){
verifyOTP(otpView.getOTP(),textviewEmail.getText().toString());
}
//then you can send verification code to server
}
});
In activity place codes after onCreate Method
declare catcher in activity
//for sms verification
public SmsVerifyCatcher smsVerifyCatcher;
parseString can be placed either in activity or in fragment
/**
* Parse verification code
*
* @param message sms message
* @return only four numbers from massage string
*/
public String parseCode(String message) {
Pattern p = Pattern.compile("\\b\\d{4}\\b");
Matcher m = p.matcher(message);
String code = "";
while (m.find()) {
code = m.group(0);
}
return code;
}
/**
* need for Android 6 real time permissions
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
smsVerifyCatcher.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
You can set phone number filter:
smsVerifyCatcher.setPhoneNumberFilter("777");
or set message filter via regexp:
smsVerifyCatcher.setFilter("<regexp>");
Library Details:-
- Github Link: https://github.com/stfalcon-studio/SmsVerifyCatcher
- License: Apache 2.0
- Version: 0.3.2