Jarvis
Jarvis

Reputation: 503

Deep Linking of android application

I need to deeplink my android application, I have searched a lot and tried various SDK's also. I want that I give a url(short url) to the user who opens the url in the browser itself. After opening the url in the browser it should redirect either to the application if installed or to the play store. I have tried "intent://view?#Intent;package=my.app.id;scheme=myapp;end;" on a page with windows.location but it never redirects me anywhere. I am able to the same if I click on a button on that page and functionality wise it is working fine, but I want to reduce a click of the user and when the url is opened it automatically redirects him/her.

Upvotes: 0

Views: 2351

Answers (1)

severin
severin

Reputation: 10268

There are different strategies to implement the automatic opening of a deep link from a browser... Unfortunately there is no method that works everywhere...

The most common one is to try to navigate to the deep link and fall back to the app store after a short while:

<script>
  // when the page is loaded...
  window.onload = function() {

    // and viewed on an Android Phone...
    if (navigator.userAgent.match(/Android/)) {

      // then try to open the deep link
      window.location.replace('myapp://foo');

      // if it didn't work after half a second, then redirect the
      // user to the app store where he can download the app
      setTimeout(function() {
        window.location.replace('http://play.google.com/store/apps/details?id=com.myapp');
      }, 500);
    }
  }
</script>

This works in the Android legacy browser and in most webviews...

For Chrome, you have to use an intent:// url. And for it to work you have to navigate there following direct user input: This means you cannot attempt to load the intent:// url automatically in a window.onload callback. The easiest way to achieve it is to redirect to the intent:// url from your server:

# This example assumes a Ruby on Rails app
def show
  if request.user_agent.match /Android/
    redirect_to 'intent://...'
  else
    render # render your website normally
  end
end

For more in-depth information read this blog post by a Google Chrome engineer: Deep App Linking on Android and Chrome.

If you do not want to deal with all this by yourself, then you could use a 3rd-party service that provides deep linking instead, e.g. branch.io or Shortcut Media (Disclaimer: I currently work at Shortcut Media).

Upvotes: 1

Related Questions