Sam Dark
Sam Dark

Reputation: 5291

Open Android app through deep link if it's installed or fall back to web if not installed

I'm trying to create a web page that automatically opens an Android app but only if app is installed. In case it's not it should redirect to another web page.

The app is already in production and is properly handling deep links like example://content?id=42. The format of the link could not be changed.

What I've already tried

1) Redirects and timeout:

window.location.replace('example://content?id=42');
setTimeout(function() {
    window.location.replace = 'http://example.com/content?id=42';
}, 500);

Works fine for iOS but for Android it redirects to example:// immediately and thus gives me ERR_UNKNOWN_URL_SCHEME. Seems to be no go for Android.

2) iframe approach. Impossible in rencent Chrome versions. Also doesn't seem to work in Samsung browser.

3) Intents with S.browser_fallback_url. Works well but in Chrome only. Doesn't work in Opera and Samsung browser... most probably nowhere else but Chrome 25+.

Upvotes: 11

Views: 9956

Answers (4)

Goose
Goose

Reputation: 111

Seems like you could at least approximate the experience by letting the user give a one-time assist:

  1. Have your web page itself have the fallback url content.
  2. When the page is hit check the user agent to see if the os is Android
  3. If its Android, show the user a choice prompt/dialog to use web or Android
  4. If they choose web (remember the choice with local storage), dismiss the dialog and show the fallback
  5. If they choose android (remember the choice), redirect to the app with the intent:// URL (without fallback_url), will take them to market to install if necessary

After the first interaction, it'll work as you describe - automatically taking them to the web page or the installed app.

Upvotes: 2

Shivam Dixit
Shivam Dixit

Reputation: 347

You can try using this scheme(to be sent to the user):

intent://details?id=X&url=Y&referrer=Z#Intent;scheme=market;action=android.intent.action.VIEW;package=com.android.vending;end";

X: Package name of the App

Y: Deep link scheme which should be defined in the App's manifest. (Please refer this) Here, they have used this URL as an example: "http://www.example.com/gizmos" , therefore Y should be replaced by this URL.

Z: Can be any data which you want to pass to the App via Google Play. Please take note that any data which you pass should not be '&' separated because the original parameters are itself '&' separated.

PS: The Google Play makes a broadcast to the app. So make sure you receive the broadcast in a receiver.

Upvotes: 0

Mimmo Grottoli
Mimmo Grottoli

Reputation: 5773

You need to be aware of the browser of the client, and its operating system and adapt your site to them. For example, if the browser is Chrome and the OS is Android, use the Intent solution; if the browser is Safari use the example:// schema. You can get the info looking at the User-Agent header of the request, but I'm sure there are many open source libraries that can help you to get infos related to browser and OS.

Upvotes: 1

Derek Fung
Derek Fung

Reputation: 8211

use http://example.com/content?id=42 as the link and add the intent filter to your activity in manifest

<intent-filter>
    <data android:scheme="http" android:host="example.com" />
    ...
</intent-filter>

However, a list of app registered, e.g. browsers, will show up when the link is first accessed on the machine.

Upvotes: 0

Related Questions