Deep linking in Android — Part 2

Gagan Batra
AndroidPub
Published in
5 min readAug 9, 2018

--

Branch Metrics

Hey guys, this tutorial is the second part of deep linking tutorials in android. If you are new to this concept/keyword, I suggest you to check out Part 1 of this series intended for absolute beginners in deep linking.

URL : Deep Linking in Android — Part 1

In this post, I will cover a very common scenario that you will encounter:

What happens if I click on a deep link when my target application is not installed ?

The process of deep linking into an application that is not yet installed is called Deferred Deep Linking. In this case, when we click on the deep link, it should ideally open the play store page of that application and allow the user to install it. However, it should parse the query parameters when your app is installed.

Deferred deep linking allows mobile developers to deliver an automated user experience, whether the application was previously installed or not. We will use Branch to implement this.

Follow the below mentioned steps to create the URL on web:

  1. Sign up/Sign in your Branch account and open the main dashboard
  2. Click Quick Links > Create link
Screenshot

3. The next screen will ask you to enter the details of this URL like name, posting methods,etc. Enter the details as per your preference. Press configure options after you are done with filling up the details.

Screenshot

4. The next screen will ask you to enter the query parameters that you wish to pass into the URL. Click + More Data button to add your custom parameters. For the sake of this tutorial, I have added one query parameter here. Click REDIRECTS tab once you are done

Note : This step is very important as it will define your business logic.

Screenshot

5. This screen will ask you to input the fall-back URL when application is not installed. I have added play store URL of my application. Do the same for rest of the two options. Press Create Link Now once you are done.

Screenshot

6. You are done with URL creation on the dashboard. Next screen will display you the generated URL that needs to be clicked.

Screenshot

Next up is handling this URL in your application. You have to integrate Branch SDK for the same. Follow the below steps:

  1. Add the following line to your app level build.gradle file:
implementation ‘io.branch.sdk.android:library:2.+’

2. Create a custom application class that extends Application class and initialize Branch SDK in it’s onCreate() method.

public final class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize the Branch object
Branch.getAutoInstance(this);
}
}

3. Add the following changes to your AndroidManifest.xml file.

  • Add a name property in your <application> tag. The value of this property should match your custom application class
android:name=”MyApplication”
  • Add the following property in your <activity> which handles deep linking.
android:launchMode=”singleTask”
  • Inside your <activity> tag, add the Branch’s URI scheme
<activity
... >
<intent-filter>
<data android:scheme=”” android:host=”open” />
<action android:name=”android.intent.action.VIEW” />
<category android:name=”android.intent.category.DEFAULT” />
<category android:name=”android.intent.category.BROWSABLE” />
</intent-filter>
</activity>
  • Add the branch key in your <application> tag. Don’t forget to add your own Branch key.
<application ... >< ...>
</...>
<! — Branch init →
<meta-data android:name=”io.branch.sdk.BranchKey” android:value=”key_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx” />
<! — Branch testing (TestMode “true” to simulate fresh installs on dev environment) →
<meta-data android:name=”io.branch.sdk.TestMode” android:value=”false” />
</application>
  • Add the following code in your Activity class which handles your deep linking and parses the data.
@Override
public void onStart() {
super.onStart();
Branch branch = Branch.getInstance();
// Branch init
branch.initSession(new Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null) {
// params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
// params will be empty if no data found
// … insert custom logic here …
Log.i(“BRANCH SDK”, referringParams.toString());
} else {
Log.i(“BRANCH SDK”, error.getMessage());
}
}
}, this.getIntent().getData(), this);
}
@Override
public void onNewIntent(Intent intent) {
this.setIntent(intent);
}

The referringParams.toString() will give you the exact same URL that was created in the dashboard in the form of JSON.

Now, the key part is to fetch the query parameters on which your entire business logic will be implemented. I struggled a lot in this part due to lack of clear documentation given by Branch.

There is REST API available on one of the documents on Github.

Endpoint :

GET /v1/url?url=<url>&branch_key=<branch key>

Parameters:

branch_key required : The Branch key of the originating app. Found in the Branch Dashboard under Settings.

url required : The URL you want to modify, including the host and domain

Example:

https://api.branch.io/v1/url?url=https://qn1e.app.link/sKuyzIMlfP&branch_key=key_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Hit this request and you will receive a response in JSON of the below format:

{
"channel": "Email",
"campaign": "test",
...
"data": {
"test_data": "123456",
...
},
"alias": "Dummy link",
"type": 0
}

Your query parameters will be stored under data key of JSON. Fetch this and implement your logic.

Following are the key scenarios covered:

  • Clicking on deep link when your app is not installed — By default, it will redirect to play store and allow you to install the application
  • Clicking on deep link when your app is installed — It will open the app, parse the query parameters and run your logic

Do give a Thumbs Up if you liked the tutorial !! :)

More tutorials in the series:

  1. Deep Linking in Android — Part 1
  2. Deep Linking in Android — Part 2 [Current]

I have created an audio recorder application in Android and I welcome everyone who is reading this post to contribute in any way you can. There is a lot of scope for improvement in this project for both Developers and QA Engineers. You can start by downloading the apk from play store and start exploring. The link is given below:

Playstore https://play.google.com/store/apps/details?id=com.odio.adfree

Sourcehttps://github.com/gbatra24/Odio

Don’t forget to check out my other posts. To read them, click here.

--

--