Skip to contentSkip to navigationSkip to topbar
On this page

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

We'll need to add a call to the sample token server, which you can deploy with just one click from the github repository(link takes you to an external page)

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
Setup your AUTHY_API_KEY.

This is how your environment should look like after the configuration

this-is-how-your-environment-should-look-like-after-the-configuration page anchor
Setup your AUTHY_API_KEY and APP_ID in your sample deployment.

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 on Android Studio. In this example we'll use retrofit.

Build.gradle

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

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 Twilio Verification SDK for Android

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

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

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 TwilioVerification callback

step-4-receive-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 <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

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
}

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.