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 <-> Branch.io 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 = BranchIO::Client.new(key, secret)
  end

  def get_client
    @client
  end
end

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'])
end

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

@deep_link_req = DeepLinking.instance.get_client.link(
  channel: "facebook",
  feature: "dashboard",
  data: {
   # DATA_YOU_WANT_TO_PASS_TO_ANDROID
  }
)

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
end

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 Branch.io SDK's dependency in the build.gradle file:
compile 'io.branch.sdk.android:library:2.+'

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">
  <intent-filter>
    <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"/>
  </intent-filter>
</activity>

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

override fun onStart() {
  super.onStart()

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

As well as the onNewIntent():

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

In Conclusion

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

Happy Deep Linking!

P.S.:
For more information, please review the Branch.io's Documentation