Auto read OTP/SMS in android
Solution 1
Google Play doesn't allow RECIEVE_SMS permission
anymore until and unless your app is default SMS handler.
So one possible solution as of now is to use SMS_RETRIEVE_API
you will need a BroadcastReceiver
and a task that does SmsRetriever.getClient(context).startSmsRetriever();
In your receiver:
if(SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
final String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
}
Solution 2
You have 3 options to automatically read the OTP SMS:
1. Read all the incoming SMS using the SMS permission:
https://androidwave.com/automatic-sms-verification-android/ http://androidbymaitri.blogspot.in/2016/08/read-sms-automatically-to-verify-otp.html
Not advised anymore, as this requires the user to explicitly grant the SMS permission.
2. Using SMS Retriever API in Google play services:
https://developers.google.com/identity/sms-retriever/overview
https://www.youtube.com/watch?v=jzWYv8y2v1c
Advised. But this requires some server level changes in the OTP SMS format. And this works only in the devices that have Play services installed.
3. Using createAppSpecificSmsToken in the SmsManager class (from Android O only):
https://code.tutsplus.com/tutorials/android-o-phone-number-verification-with-sms-token--cms-29141
Not advised, because this works only in Android O, as of now.
Solution 3
Using SmsVerifyCatcher library
-
In
manifest
, add these permissions<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" />
-
In
build.gradle
(app gradle)implementation 'com.github.stfalcon:smsverifycatcher:0.3.2'
-
Initialize
SmsVerifyCatcher
in onCreate activitysmsVerifyCatcher = new SmsVerifyCatcher(getActivity(), new OnSmsCatchListener<String>() { @Override public void onSmsCatch(String message) { String code = parseCode(message);//Parse verification code Log.d("Agilanbu OTP", code); Toast.makeText(getActivity(), "Agilanbu OTP: " + code, Toast.LENGTH_LONG).show(); et_otp.setText(code);//set code in edit text } });
-
In activity lifecycle
@Override protected void onStart() { super.onStart(); smsVerifyCatcher.onStart(); } @Override protected void onStop() { super.onStop(); smsVerifyCatcher.onStop(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); smsVerifyCatcher.onRequestPermissionsResult(requestCode, permissions, grantResults); }
-
Parse the message
private String parseCode(String message) { Pattern p = Pattern.compile("\\b\\d{6}\\b"); Matcher m = p.matcher(message); String code = ""; while (m.find()) { code = m.group(0); } return code; }
Related videos on Youtube
Dinesh
Updated on June 04, 2022Comments
-
Dinesh almost 2 years
I am working on an Android App, in which server sends an OTP and the user needs to enter this OTP in the App, to SignUp for my App. What I want is, that my App should be able to automatically read the OTP sent by the server. How can I achieve this? Any help or guidance in this regard would be highly appreciated.
Thanks..! In advance
-
CommonsWare almost 7 years"How can I achieve this?" -- talk to the maintainers of the server and ask them. They are the only ones who know how their "server sends an OTP", whether it is appropriate for you to "automatically read the OTP sent by the server", etc.
-
-
Uzair Mughal over 2 yearsThis library is not good to use now as Play Console ask you to provide reason and proper use-case to use read-sms permission.
-
Avinash Shinde over 2 yearsPlease check below link :- Permissions and APIs that Access Sensitive Information support.google.com/googleplay/android-developer/answer/9888170