How to Integrate Truecaller Login in Android App

Truecaller SDK offers one-tap login without the need for manual phone number entry or OTP verification. This simplifies the onboarding process, enabling users to sign in or register quickly and efficiently. The SDK automatically populates verified user details (such as name, phone number, and email) from Truecaller’s database, saving users the hassle of filling out lengthy forms and reducing drop-off rates during sign-up. It also reduces the cost involved in sending OTP SMS.


In this article, we'll see how Truecaller SDK can be integrated in your android app's login flow.

1. Truecaller Client ID

To get started, first you need to sign up on truecaller's developer page and get the Client ID.
  1. Visit https://sdk-console-noneu.truecaller.com/sign-up and create a new account.
  2. Once the account is created, click on New Project and fillout the details.
  3. After creating the project, expand the Credentials section and give the app details like platform, package name and SHA1 fingerprint. If your app is on playstore, you need to give SHA1 fingerprint from play console. After filling the required details, the Client ID will be generated
  4. Expand the Consent section fill out the details
  5. Add Test mobile numbers to test the flow while the project in test phase.
  6. Once everything is filled, you can submit the app for approval if you think app is ready to be published on play store

2. Android Truecaller Login Flow

Once you have the Cliend ID generated, let's move onto android integration part.
  1. Open the app's build.gradle file and add the truecaller dependency and sync the project.
    build.gradle
    
    dependencies {
        ...
            
        implementation "com.truecaller.android.sdk:truecaller-sdk:3.1.0"
    }
      
  2. Add your Truecaller client ID to strings.xml
    strings.xml
    <resources>
    	<string name="app_name">truecaller-login</string>
    
    	<!-- Truecaller Client ID -->
    	<string name="truecaller_client_id">mtaivooladzibn-aburiz4up06-dix5kjg9jlfye2p0</string>
    
    
    	<string name="truecaller_cant_use_error">Truecaller login is not supported on this device!</string>
    	<string name="truecaller_code_challange_error">Unable to use Truecaller login!</string>
    </resources>
        
  3. Open AndroidManifest.xml and add uses-sdk, add INTERNET permission and add truecaller meta-data.
    AndroidManifest.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <uses-sdk tools:overrideLibrary="com.truecaller.android.sdk" />
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            ....
            tools:targetApi="31">
    
            <meta-data
                android:name="com.truecaller.android.sdk.ClientId"
                android:value="@string/truecaller_client_id" />
    
            ....
        </application>
    
    </manifest>
        
  4. Create a new activity called LoginActivity and add the below layout. Here we are creating a simple login screen with a button that triggers the truecaller login.
  5. Open LoginActivity and do the follwing changes. Here
    • initTruecaller() method initialises the truecaller SDK by supplying necessary params.
    • canUseTrueCaller() method checks whether truecaller can be used on this device or not. If this returns false, you should fallback to other login options.
    • If the device supports truecaller login, calling getAuthorizationCode() starts the login flow
    • Implement the acitivty from TcOAuthCallback that provides onSuccess(), onFailure() methods to know the status of login.
    • Once the login is successful, you will receive the authorizationCode & state in onSuccess().
    You can run the app now and test it once. If everything is configured properly, you should see below flow.

3. Fetching User Details on Backend

Once user login flow is done, truecaller shares state, authorizationCode in onSuccess() method. You should send these details along with codeVerifier to your backend to verify / fetch user details from truecaller server. More information on this topic can be found here

References

  • Generating truecaller Client ID - link
  • More info on customising the truecaller dialog - link
  • Vector image used in this example
Let me know if you have any queries in the comments section below.

Cheers!
Happy Coding 🤗
Previous Post Next Post

نموذج الاتصال