Integrating Twilio Verification SDK for Android using the sample backend
This guide is part of the Twilio Verification SDK integration guides
For the full flow to be implemented, you must deploy your own JWT provider. The service will receive phone numbers and return signed JWTs, using your AUTHY_API_KEY
as encoding key.
First, we’ll integrate the SDK against a sample backend that we’ve already set up for you.
Step 1: deploy a sample backend to begin the integration
We’ll need to add a call to the sample token server, which you can deploy with just one click from the github repository
Then you will need to set your AUTHY_API_KEY
and your APP_ID
in the heroku app as an environment variable.
Note:
- If you need help finding your AUTHY_API_KEY, please follow these steps
- If you need help finding your APP_ID, please follow these steps
This is how your environment should look like after the configuration.
Step 2: query your sample token server to transform phone numbers into JWTs
Create a new blank-activity project on Android Studio. In this example we’ll use retrofit.
Build.gradle
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
Retrofit interface: TokenServerApi.java
public interface {
@POST("/verify/token")
@FormUrlEncoded
Call<TokenServerResponse> getToken(@Field("phone_number") String phoneNumber);
}
Response holder: TokenServerResponse.java
public class TokenServerResponse {
@SerializedName("jwt_token")
private String jwtToken;
public String getJwtToken() {
return jwtToken;
}
public void setJwtToken(String jwtToken) {
this.jwtToken = jwtToken;
}
}
In the main activity initialize the service. Replace the TOKEN_SERVER_URL
string with your deployed sample backend.
private TokenServerApi tokenServerApi;
private void initTokenServerApi() {
String TOKEN_SERVER_URL = "https://verification-token.herokuapp.com";
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(TOKEN_SERVER_URL)
.build();
tokenServerApi = retrofit.create(TokenServerApi.class);
}
Make the call to the sample token server. This server will receive a phone number and return a JWT which will be used to verify the authenticity of the requester.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String numberToVerify = "1555555555"; //Should come from user input
tokenServerApi
.getToken(numberToVerify)
.enqueue(new Callback<TokenServerResponse>() {
@Override
public void onResponse(Call<TokenServerResponse> call,
Response<TokenServerResponse> response) {
String jwtToken = response.body().getJwtToken();
}
@Override
public void onFailure(Call<TokenServerResponse> call, Throwable t) {
throw new RuntimeExecutionException(t); //Woops!
}
});
}
});
Step 3: add Twilio Verification SDK for Android
Add the SDK into your module's build.gradle
compile 'com.twilio:verification:+'
Note: The SDK is exposed via jCenter repository, so make sure you add that repository in your repository list. In your app’s build.gradle
allprojects {
repositories {
jcenter()
(...)
}
}
Create a TwilioVerification instance. Keep a reference in your activity or presenter
private TwilioVerification twilioVerification;
Instantiate it in onCreate(). The constructor will require a context.
twilioVerification = new TwilioVerification(this);
Add the start verification call when JWT is received
public void onResponse(Call<TokenServerResponse> call,
Response<TokenServerResponse> response) {
String jwtToken = response.body().getJwtToken();
twilioVerification.startVerification(jwtToken, Via.SMS);
}
Step 4: receive TwilioVerification callback
When the user's device receives a phone verification SMS from Twilio, Google Play services will automatically pass it to Twilio Verification SDK for validation. This will let your app know when the SDK has a response.
Androidmanifest.xml
(Inside <application>
tag)
<receiver android:name=".MyVerificationReceiver" >
<intent-filter>
<action android:name="com.twilio.verification.current_status" />
</intent-filter>
</receiver>
MyVerificationReceiver.java
public class MyVerificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
VerificationStatus verificationStatus = TwilioVerification.getVerificationStatus(intent);
// NOT_STARTED, STARTED, AWAITING_VERIFICATION, SUCCESS, ERROR
state = VerificationStatus.State
}
}
Next step: Integrate Twilio Verification with your own backend
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.