Integrating Branch.io on Android Platform

Posted By : Karan Jeet Singh | 23-Dec-2018

Integrating Branch.io on Android Platform

 

Introduction

Let’s just say you catch up with a friend on weekends and they tell you about an exclusive app that provides you some monetary or in-app benefits that help them advance and immediately asks you to sign up right there. And you did, you are hooked with it and now you refer that app to others. But ever wondered how that entire stuff works? To understand that, a tool called Branch.io comes in handy. Branch.io is a full-fledged tool that allows a user to access its dashboard where a custom link and its properties can be generated so that clicking on that links performs a prescribed action accordingly.

 

Prerequisite

  • Hands on experience on Android Studio, and
  • Good knowledge of Java and its concepts?

 

Setting up Branch.io

After signing up on Branch.io, the first thing you’ll stumble upon is a detailed dashboard where you’ll be asked to register you Android app and go to Link Settings and further Android Settings. Deepviews will show how the preview of you app will look when the link of your Branch.io Android app will be clicked.

 

By the clear look of the screenshot above, you can choose your application when the radio button is set on ‘Google Play Search’ and see which Android app is yours to integrate further. Or you can provide a custom URL for beta testing. Once you are done configuring the Link Settings. And then at the bottom of the same Link Settings page, check up on Link Domain to satiate the default or alterative link domain.

 

Branch.io will also allow you to use your own domain in order to make it feel more native.  Now you can configure your Deepviews. Deepviews will show you a preview of how your app view will look when the link generated and shared by your android app is clicked.  When clicked on Get App, the user will be redirected to Google Play App on Google Play store.

 

 Setup in Android Studio

First thing, we are going to import Branch.io in build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation ('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:25.2.0'
    implementation 'com.android.support:design:25.2.0'

    // required
    implementation 'io.branch.sdk.android:library:3.+'

    // optional
    implementation 'com.android.support:customtabs:23.3.0' // Chrome Tab matching
    implementation 'com.google.android.gms:play-services-ads:9+' // GAID matching
    implementation 'com.google.android.gms:play-services-appindexing:9.+' // App indexing

    testImplementation 'junit:junit:4.12'
}

If you already have the Android App for which you are integrating Branch for, then you must add a Deeplinking module in your AndroidManifest.xml,
 

<activity
android:name=".LauncherActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">

 
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

 
<!-- Branch App Links -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="8v90.app.link" />
<data android:scheme="https" android:host="8v90-alternate.app.link" />
</intent-filter>
</activity>
<activity
android:name=".LauncherActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">

 
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

 
<!-- Branch App Links -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="8v90.app.link" />
<data android:scheme="https" android:host="8v90-alternate.app.link" />
</intent-filter>
</activity>
This piece of code will fetch the custom parameters that have been added while Link Properties are created. Notice how we are reading the userId and platform keys. The link generated is randomly but the prefix remains the same as entered in Dashboard.
 
To create those two parameters, thats how we can create Link Properties:
@Override
    public void onStart() {
        super.onStart();
        // Branch init
        Branch.getInstance().initSession(new Branch.BranchReferralInitListener() {
            @Override
            public void onInitFinished(JSONObject referringParams, BranchError error) {
                if (error == null) {
                    Log.i("BRANCH SDK", referringParams.toString());
Log.i("Custom User id", referringParams.optString(userId));
Log.i(Platform, referringParams.optString(platform));
 
                    // Retrieve deeplink keys from 'referringParams' and evaluate the values to determine where to route the user
                    // Check '+clicked_branch_link' before deciding whether to use your Branch routing logic
                } else {
                    Log.i("BRANCH SDK", error.getMessage());
                }
            }
        }, this.getIntent().getData(), this);
    }
    @Override
    public void onNewIntent(Intent intent) {
        this.setIntent(intent);
    }

LinkProperties lp = new LinkProperties()
    .setChannel("facebook")
    .setFeature("sharing")
    .setCampaign("content 123 launch")
    .setStage("new user")
    .addControlParameter("$desktop_url", "http://example.com/home")
    .addControlParameter("userId", "23")
    .addControlParameter("platform",ANDROID));
 
buo.generateShortUrl(this, lp, new Branch.BranchLinkCreateListener() {
    @Override
    public void onLinkCreate(String url, BranchError error) {
        if (error == null) {
            Logger.LogInfo("success","generated");
        }
    }
});

 

NOTE: The Branch must only be initialized in the Launcher Activity

 

Scope

The scope of Branch.io is huge since giants like Imgur, MakemyTrip, Tinder and reddit etc. rely on it for sharing their stuff and content. Once you are good to go with the pricing, even your medium sized application can integrate it promote it to increase its reach likewise.

 

Conclusion

On a closing note, it has been observed that Branch.io allows it’s every user to control every aspect of the link that is being generated. And there are no such downfalls of it found. It is easy to control and configure.

 

References

Request for Proposal

Recaptcha is required.

Sending message..