7

I have a deeplink defined for my Android app in the manifest file:

  <activity android:name="com.example.DeeplinkActivity"
        android:screenOrientation="portrait"
        android:theme="@style/MyBaseTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />

                         <!-- Accepts URIs that begin with "example://shelf” -->
            <!-- Currently handles Ads deeplink structure (iPhone structure) -->
            <data
                android:host="shelf"
                android:pathPrefix=""
                android:scheme="example" />

            <!-- Accepts URIs that begin with "example://com” -->
            <data
                android:host="com"
                android:pathPrefix=""
                android:scheme="example" />

            <!-- Accepts URIs that begin with http://www.example.com/some/sample/page.htm” -->
            <data
                android:host="www.example.com"
                android:pathPrefix="/some/sample/page.htm"
                android:scheme="http" />
        </intent-filter>
    </activity>

I have also some links in my app that look similar but should NOT be treated as deeplinks. They do begin with http://www.example.com but they have a completly different prefix. For example: http://www.example.com/other/not/deep/link.htm .

For some reason the intent filter defined for DeeplinkActivity is being trigerred even though it is defined with the prefix "/some/sample/page.htm".

Does the prefix being ignored? if not why one should use the the pathPrefix attribute when defining the deeplink intent filter?

Maxim Rahlis
  • 843
  • 9
  • 26
  • Can you please make sure that the link's href is pointing the pathprefix you have registered – Dinash Jun 23 '15 at 14:10
  • Yes. The links (href is valid...) I used to check it (inside & outside the app) as well as the the urls I launch within my app (to be opened with any installed browser) are all valid. This must be something to do with the manifest intent filter I guess. Do you have any sample code that does not ignore the pathPrefix attribute while deciding if to qualify for the activity's intent filter? – Maxim Rahlis Jun 24 '15 at 07:49
  • I checked your manifest intent filter in my sample app and it worked as expected. So thought it could something with the href pointing to wrong one – Dinash Jun 24 '15 at 10:04
  • Dinash, thanks for the heads-up. When you said the example snippet I posted actually works. I rechecked it myself with a clean project and It worked. So, then I knew this must be something with the other data tags getting in my way... – Maxim Rahlis Jul 07 '15 at 15:15

2 Answers2

5

Removing the pathPrefix didn't solve the problem for me, I either ended up with all http deep links working or none of them, regardless of prefix. It seems like the prefixes, hosts and schemes all bleed into eachother, so with your example example://www.example.com/ would probably also trigger a deep link even though none of the individual data elements define it. I ended up figuring out that you can just separate them into different intent-filters and they wont mix.

So in your case you could use:

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />

                     <!-- Accepts URIs that begin with "example://shelf” -->
        <!-- Currently handles Ads deeplink structure (iPhone structure) -->
        <data
            android:host="shelf"
            android:pathPrefix=""
            android:scheme="example" />

        <!-- Accepts URIs that begin with "example://com” -->
        <data
            android:host="com"
            android:pathPrefix=""
            android:scheme="example" />

    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />

        <!-- Accepts URIs that begin with http://www.example.com/some/sample/page.htm” -->
        <data
            android:host="www.example.com"
            android:pathPrefix="/some/sample/page.htm"
            android:scheme="http" />

    </intent-filter>

this will only accept http URIs that begin with http://www.example.com/some/sample/page.htm OR URIs begining with example://com or example://shelf

so in your originial question, http://www.example.com/other/not/deep/link.htm will not trigger a deep link.

JStephen
  • 1,059
  • 3
  • 13
  • 28
  • You are correct they do blend. It was a while ago and I have evantually used this approach. I ended up with 3 different intent filter for the same deeplink activity. – Maxim Rahlis Dec 15 '15 at 20:12
  • 1
    I use two different intent-filters. One with android:autoVerify="true" to enable app link introduced in Marshmallow. The another one with android:pathPrefix="/abc/" to enable deep link from browser like Chrome. Using one intent-filter with different does screw up all scenarios. – Song Jan 28 '16 at 01:06
  • pathPrefix cannot be empty!! – Yessine Mahdouani Jun 21 '18 at 09:16
4

Apparently, an empty android:pathPrefix attribute in some other data tags will cause specific data tag (the last data tag in above question) to ignore its own pathPrefix even though it is well defined!

So this manifest declaration fixes the last data tag to behave normally:

  <activity android:name="com.example.DeeplinkActivity"
    android:screenOrientation="portrait"
    android:theme="@style/MyBaseTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />

                     <!-- Accepts URIs that begin with "example://shelf” -->
        <!-- Currently handles Ads deeplink structure (iPhone structure) -->
        <data
            android:host="shelf"
            android:scheme="example" />

        <!-- Accepts URIs that begin with "example://com” -->
        <data
            android:host="com"
            android:scheme="example" />

        <!-- Accepts URIs that begin with http://www.example.com/some/sample/page.htm” -->
        <data
            android:host="www.example.com"
            android:pathPrefix="/some/sample/page.htm"
            android:scheme="http" />
    </intent-filter>
</activity>
Maxim Rahlis
  • 843
  • 9
  • 26