The SendOtp Verification SDK makes verifying phone numbers easy. SDK supports the verification of phone numbers via SMS & Calls.
Just add the "compile 'com.msg91.sendotp.library:library:2.9'" in your build.gradle of your module.
dependencies {
...
compile 'com.msg91.sendotp.library:library:2.9'
...
}
grab via Maven:
<dependency>
<groupId>com.msg91.sendotp.library</groupId>
<artifactId>library</artifactId>
<version>2.9</version>
<type>pom</type>
</dependency>
Note:
After login at sendOTP click on create new application fill app details
Note:
- Select Android as Application Type
- Package Name is unique and it's not editable.
- In the key field set your debug version key hash while you are in development mode.
- In the key field set your release version key hash while you are in production mode.
Note: implement 'VerificationListener' in your activity or fragment to get result call backs.
create instance of Verification as a class variable and initialise it by passing country code and mobile number.
mVerification = SendOtpVerification.createSmsVerification(this, "set_phone_number", this, "set_country_code");
mVerification.initiate(); //sending otp on given number
Use instance of Verification to call verify method pass received OTP here.
mVerification.verify("verification_code"); //verifying otp for given number
As a Result of each request any one of method will triggered.
@Override
public void onInitiated(String response) {
Log.d(TAG, "Initialized!" + response);
//control comes when OTP successfully sent.
}
@Override
public void onInitiationFailed(Exception exception) {
Log.e(TAG, "Verification initialization failed: " + exception.getMessage());
//control comes when OTP failed successfully sent.
}
@Override
public void onVerified(String response) {
Log.d(TAG, "Verified!\n" + response);
//control comes when OTP verified successfully.
}
@Override
public void onVerificationFailed(Exception exception) {
Log.e(TAG, "Verification failed: " + exception.getMessage());
//control comes when OTP verification failed.
}
- If OTP not working in release mode but working in debug/test environment please replace your secret key of release version at sendOTP [to get secret key search logs of android studio by 'UserHeaders' or use this code snippet to generate]
- If you are getting exception message 'Request Unsuccessful Try Again' it is because device not supporting 'https' , Solution is to set extra parameter true in createSmsVerification like :
mVerification = SendOtpVerification.createSmsVerification(this, phoneNumber, this, countryCode, true);
Copyright 2017 MSG91
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.