How to Disable Android App links if user is on website?

Viewed 4818

Let us say there is a website https://www.example.com and we are listening to https://www.example.com/product in Android App with following intent filter

<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />

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

    <data
        android:host="www.example.com"
        android:pathPrefix="/product"
        android:scheme="https" />
</intent-filter>

And the file assetlinks.json is also hosted on example.com

The Android App links are working fine as expected.

Now, if the user went to browser and open example.com and the user clicks on https://example.com/product which will be opened in app but we want to block app from opening only if the user is on our website

Any help will be appreciated.

3 Answers

After looking a lot, The only workaround i was able to find to open web instead of app is redirection from web -> app (redirection logic) -> website.

An important thing, I noticed is that the App is never invoked if the page is developed in React or Angular and is not based on JSP. In our case, We were using Tomcat server, So whenever user navigate to different page, The Android System get to know that a new page is opened in browser, which makes Android System to open Android App if listening to that url specified in the intent filter. However, this doesn't happen if the page is designed in React and Angular.

Now, this is really difficult for me to describe as i am not a web developer and this is what the web developers told and i experienced with both JSP and React pages.

I think the web-developers might have told you cheese ...because it's all about the URL and not the content. Tap-hold in Chrome and then "open in new tab" works without passing the new URL through the intent filter... and you'll fail to read the URL from Chrome, therefore you can ditch that approach.

All you can do to work around the problem you've created by adding the intent-filter would be to setup two sub-domains and then filter only for one for them, while still displaying the same content on both of them (needs a canonical meta tag in HTML) - then you can simply use the page as usual and nothing will trigger the intent filter.

And in order to still be able to open the app trough the intent filter ... just add a button, which links to the other one sub-domain, which the intent filter is matching. Other approaches might be illogical or otherwise problematic.

Well, based on official Android documents, you need to remove BROWSABLE category from intent-filter on Manifest.

https://developer.android.com/training/app-links/deep-linking

From document for who have difficulties to open a url and read a document :

Include the BROWSABLE category. It is required in order for the intent filter to be accessible from a web browser. Without it, clicking a link in a browser cannot resolve to your app.

That means, if you don't want browser to direct to your app, JUST remove BROWSABLE category. That's all.

So your Manifest will look like below :

<intent-filter android:autoVerify="true"
tools:ignore="AppLinkUrlError">

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

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

<data
    android:host="www.example.com"
    android:pathPrefix="/product"
    android:scheme="https" />
</intent-filter>

You will get an "AppLinkUrlError" so you can add;

tools:ignore="AppLinkUrlError"

to suppress it.

Related