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.
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:
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
_10public interface {_10 @POST("/verify/token")_10 @FormUrlEncoded_10 Call<TokenServerResponse> getToken(@Field("phone_number") String phoneNumber);_10}
Response holder: TokenServerResponse.java
_12public 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 });
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
_10allprojects {_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
_10public void onResponse(Call<TokenServerResponse> call, _10 Response<TokenServerResponse> response) {_10 String jwtToken = response.body().getJwtToken();_10 twilioVerification.startVerification(jwtToken, Via.SMS);_10}
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}