Deep Linking with Branch, Rails server and Android client

Today I will share with you how to generate deep links from your rails server and handle them in an Android application.

First we will start with the server's implementation.

Rails Server

For the Rails <-> communication I used the branch_io gem which is really easy and straightforward to use.

I created a separate singleton utility class for handling the initialization of the branch client:

require 'singleton'

class DeepLinking
  include Singleton

  def init(key, secret)
    @client =, secret)

  def get_client

For the hosting of the web app I'm using the Heroku's service so I decided to create environment variables for the Branch's key and secret directly in the Heroku's dashboard and use them in the app when initializing the client.
The initialization happens in the application.rb file within the Application class:

config.after_initialize do
  DeepLinking.instance.init(ENV['BRANCH_KEY'], ENV['BRANCH_SECRET'])

So whenever I want generate a branch link, I just call the DeepLinking class like this:

@deep_link_req =
  channel: "facebook",
  feature: "dashboard",
  data: {

So, now we have to check if the link creation succeeds and return the newly generated deep link:

if @deep_link_req.success?
 return @deep_link_req.url

Branch Link Settings

Now we have to make some updated to the default link settings. This takes place in the Dashboard's Link Settings page.

We're interested in the following settings:

  • Check the Always try to open app setting at the top
  • Android redirects:
    • Check the I have an Android App option
    • Android URI Scheme should be something like this: myapp://
    • Custom URL should be checked and the url where the user will be redirected if the URI scheme fails (for example, the PlayStore app's page). Also the package name must be the one specified in the app's project.

Android App

First, we have to include the SDK's dependency in the build.gradle file:
compile ''

Then, we have to add an intent-filter to the activity we will use to handle the branch links in its Manifest's declaration:

<activity android:name=".HomeActivity">
    <data android:scheme="myapp" android:host="open" />

    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>

Now, we go to our HomeActivity and override the onStart() method:

override fun onStart() {

  val branch = Branch.getInstance(this)
  branch.initSession ({ referringParams, error ->
            // Handle the received data
        },, this)

As well as the onNewIntent():

override fun onNewIntent(intent: Intent?) {
  this.intent = intent

In Conclusion

That is it! Everything you have to do to generate and handle deep links! 👻

Happy Deep Linking!

For more information, please review the's Documentation