Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Deep Linking doesn't work in all cases and on all browsers

I created an intent filter to one of my activities in order to be able to launch it via deep linking :

     <activity
        android:name="com.myapp.activities.LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:host="myserver.com"
                android:pathPrefix="/product/"
                android:scheme="https" />
            <data
                android:host="www.myserver.com"
                android:pathPrefix="/product/"
                android:scheme="https" />
        </intent-filter>
    </activity>

Mostly everything works ok. When im clicking these links from any email client, or from whatsapp messages, or from tweets, picking any browser in the app chooser dialog, it identifies my intent filter and opens the right activity. Which means my intent filter was defined ok.

But... there are some cases where i need to click these links from the device's calendar or from HTMLViewer. In these cases my app is not opened when i select the stock browser. It just opens the browser with the URL address. This just happens when i select the device's stock browser. When i select a different browser (e.g Chrome) the app is opened correctly. Any idea why is that ? Or how can i enable deep linking to work in all cases ?

2 important notes :

  1. Im testing this for now on Galaxy S3. I know the browser on Galaxy devices is not considered "stock" per se. Still i would like to find a solution that will work on all devices.

  2. The url address for the deep linking is an actual address that runs a javascript on the browser side. For cases where the app is not installed on the user's device it redirects him to the play store. Im saying that because im also open to server side solutions if they might solve this issue. I saw this Question but the answer there doesn't work for me.

Anyway, I really appreciate any help from the SO community. This has been a pain in the ass for me (and im sure for others).

Thanks.

like image 244
AsafK Avatar asked Oct 31 '22 21:10

AsafK


1 Answers

I can't see anything obviously wrong with what you have posted but you say:

In these cases my app is not opened when i select the stock browser.

is your app not appearing as an option to choose from when you click the link? If your filter matches it should be an option no? If you select the default browser I don't blame it for swallowing the intent.

Anyway, if you cannot get it working, you could use some client side web code to have the urls fire out to the app if installed and off to the store if not, and modify the URI scheme to your own custom one. See this answer:

https://stackoverflow.com/a/8237102/1137254

Then you could maybe filter for both:

<activity
    android:name="com.myapp.activities.LoginActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="myserver.com"
            android:pathPrefix="/product/"
            android:scheme="https" />
        <data
            android:host="www.myserver.com"
            android:pathPrefix="/product/"
            android:scheme="https" />
        <data
            android:host="product"
            android:scheme="asafk" />
    </intent-filter>
</activity>

UPDATE: I ran a test on a galaxy s3 myself and found that your pathPrefix is set a little wrong. Your current filter works with a URL like this https://myserver.com/product/blah but not https://myserver.com/product

So here is your update intent filter (notice the trailing forward slash is omitted):

 <activity
    android:name="com.myapp.activities.LoginActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="myserver.com"
            android:pathPrefix="/product"
            android:scheme="https" />
        <data
            android:host="www.myserver.com"
            android:pathPrefix="/product"
            android:scheme="https" />
    </intent-filter>
</activity>

Please let me know if this solves your problem. Your user may still select the wrong option so I still recommend following my other recommendation of having some Javascript on the page that attempts to open app (it's pretty basic but I think it's widely used for this).

Hope this fixes your problem.

like image 70
Sam Avatar answered Nov 11 '22 03:11

Sam