Skip to contentSkip to navigationSkip to topbar
Rate this page:
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


_10
compile 'com.squareup.retrofit2:retrofit:2.2.0'
_10
compile 'com.squareup.retrofit2:converter-gson:2.2.0'

Retrofit interface: TokenServerApi.java


_10
public interface {
_10
@POST("/verify/token")
_10
@FormUrlEncoded
_10
Call<TokenServerResponse> getToken(@Field("phone_number") String phoneNumber);
_10
}

Response holder: TokenServerResponse.java


_12
public class TokenServerResponse {
_12
@SerializedName("jwt_token")
_12
private String jwtToken;
_12
_12
public String getJwtToken() {
_12
return jwtToken;
_12
}
_12
_12
public void setJwtToken(String jwtToken) {
_12
this.jwtToken = jwtToken;
_12
}
_12
}

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


_12
private TokenServerApi tokenServerApi;
_12
_12
private void initTokenServerApi() {
_12
String TOKEN_SERVER_URL = "https://verification-token.herokuapp.com";
_12
_12
Retrofit retrofit = new Retrofit.Builder()
_12
.addConverterFactory(GsonConverterFactory.create())
_12
.baseUrl(TOKEN_SERVER_URL)
_12
.build();
_12
_12
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.


_22
button.setOnClickListener(new View.OnClickListener() {
_22
@Override
_22
public void onClick(View v) {
_22
String numberToVerify = "1555555555"; //Should come from user input
_22
_22
tokenServerApi
_22
.getToken(numberToVerify)
_22
.enqueue(new Callback<TokenServerResponse>() {
_22
_22
@Override
_22
public void onResponse(Call<TokenServerResponse> call,
_22
Response<TokenServerResponse> response) {
_22
String jwtToken = response.body().getJwtToken();
_22
}
_22
_22
@Override
_22
public void onFailure(Call<TokenServerResponse> call, Throwable t) {
_22
throw new RuntimeExecutionException(t); //Woops!
_22
}
_22
});
_22
}
_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


_10
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


_10
allprojects {
_10
repositories {
_10
jcenter()
_10
(...)
_10
}
_10
}

Create a TwilioVerification instance. Keep a reference in your activity or presenter


_10
private TwilioVerification twilioVerification;

Instantiate it in onCreate(). The constructor will require a context.


_10
twilioVerification = new TwilioVerification(this);

Add the start verification call when JWT is received


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


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)


_10
<receiver android:name=".MyVerificationReceiver" >
_10
<intent-filter>
_10
<action android:name="com.twilio.verification.current_status" />
_10
</intent-filter>
_10
</receiver>

MyVerificationReceiver.java


_10
public class MyVerificationReceiver extends BroadcastReceiver {
_10
@Override
_10
public void onReceive(Context context, Intent intent) {
_10
VerificationStatus verificationStatus = TwilioVerification.getVerificationStatus(intent);
_10
_10
// NOT_STARTED, STARTED, AWAITING_VERIFICATION, SUCCESS, ERROR
_10
state = VerificationStatus.State
_10
}
_10
}



Rate this page: