Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

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

step-1-deploy-a-sample-backend-to-begin-the-integration page anchor

To begin, add a call to the sample token server, which you can deploy with one click from the GitHub repository(link takes you to an external page).

Then set your AUTHY_API_KEY and your APP_ID in the Heroku app as environment variables.

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
Deploy Authy SDK Backend with options for app name and runtime selection.

Environment after configuration

environment-after-configuration page anchor
Config variables setup with APP_ID, AUTHY_API_KEY, LANG, and RACK_ENV fields.

Step 2: Query your sample token server to transform phone numbers into JWTs

step-2-query-your-sample-token-server-to-transform-phone-numbers-into-jwts page anchor

Create a new blank-activity project in Android Studio. This example uses Retrofit.

Build.gradle

buildgradle page anchor
1
compile 'com.squareup.retrofit2:retrofit:2.2.0'
2
compile 'com.squareup.retrofit2:converter-gson:2.2.0'

Retrofit interface: TokenServerApi.java

1
public interface {
2
@POST("/verify/token")
3
@FormUrlEncoded
4
Call<TokenServerResponse> getToken(@Field("phone_number") String phoneNumber);
5
}

Response holder: TokenServerResponse.java

TokenServerResponse.java

tokenserverresponsejava page anchor
1
public class TokenServerResponse {
2
@SerializedName("jwt_token")
3
private String jwtToken;
4
5
public String getJwtToken() {
6
return jwtToken;
7
}
8
9
public void setJwtToken(String jwtToken) {
10
this.jwtToken = jwtToken;
11
}
12
}

In the main activity initialize the service. Replace the TOKEN_SERVER_URL string with your deployed sample backend.

1
private TokenServerApi tokenServerApi;
2
3
private void initTokenServerApi() {
4
String TOKEN_SERVER_URL = "https://verification-token.herokuapp.com";
5
6
Retrofit retrofit = new Retrofit.Builder()
7
.addConverterFactory(GsonConverterFactory.create())
8
.baseUrl(TOKEN_SERVER_URL)
9
.build();
10
11
tokenServerApi = retrofit.create(TokenServerApi.class);
12
}

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.

1
button.setOnClickListener(new View.OnClickListener() {
2
@Override
3
public void onClick(View v) {
4
String numberToVerify = "1555555555"; //Should come from user input
5
6
tokenServerApi
7
.getToken(numberToVerify)
8
.enqueue(new Callback<TokenServerResponse>() {
9
10
@Override
11
public void onResponse(Call<TokenServerResponse> call,
12
Response<TokenServerResponse> response) {
13
String jwtToken = response.body().getJwtToken();
14
}
15
16
@Override
17
public void onFailure(Call<TokenServerResponse> call, Throwable t) {
18
throw new RuntimeExecutionException(t); //Woops!
19
}
20
});
21
}
22
});

Step 3: Add the Twilio Verification SDK for Android

step-3-add-the-twilio-verification-sdk-for-android page anchor

Add the SDK to your module's build.gradle:

compile 'com.twilio:verification:+'

Note: The SDK is exposed via the jCenter repository, so add that repository to your repository list in your app's build.gradle:

1
allprojects {
2
repositories {
3
jcenter()
4
(...)
5
}
6
}

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

1
public void onResponse(Call<TokenServerResponse> call,
2
Response<TokenServerResponse> response) {
3
String jwtToken = response.body().getJwtToken();
4
twilioVerification.startVerification(jwtToken, Via.SMS);
5
}

Step 4: Receive the TwilioVerification callback

step-4-receive-the-twilioverification-callback page anchor

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 the <application> tag):

1
<receiver android:name=".MyVerificationReceiver" >
2
<intent-filter>
3
<action android:name="com.twilio.verification.current_status" />
4
</intent-filter>
5
</receiver>

MyVerificationReceiver.java

MyVerificationReceiver.java

myverificationreceiverjava page anchor
1
public class MyVerificationReceiver extends BroadcastReceiver {
2
@Override
3
public void onReceive(Context context, Intent intent) {
4
VerificationStatus verificationStatus = TwilioVerification.getVerificationStatus(intent);
5
6
// NOT_STARTED, STARTED, AWAITING_VERIFICATION, SUCCESS, ERROR
7
state = VerificationStatus.State
8
}
9
}

Integrate Twilio Verification with your own backend.