Android Deep Linking with custom URI
Asked Answered
S

3

12

I have the following defined in my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.package">
...
    <activity
        android:name="app.myActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <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="www.example.com"
                android:pathPrefix="/gizmos"
                android:scheme="http" />
            <!-- note that the leading "/" is required for pathPrefix-->
            <!-- Accepts URIs that begin with "example://gizmos”-->
            <data
                android:host="gizmos"
                android:scheme="example" />
        </intent-filter>
    </activity>
 ...

And I have defined my onCreate() as such:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    Uri data = intent.getData();
    if (data != null) {
        Log.d("URI",data.toString());            
    }
}

This is in accordance with the Android documentation: Android Deep Linking

So the question is:

How do I test the URI deep linking? According to the documentation I run something like

adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos" com.app.package

But this produces:

Error: Activity not started, unable to resolve Intent { act=android.intent.action.VIEW dat=example://gizmos flg=0x10000000 pkg=com.app.package }

I have also tried the shell with the name and reference of the activity, the launcher activity and left the package blank. The only one I can get to work is:

adb shell am start -W -a android.intent.action.VIEW -d "http://www.example.com/gizmos"

But even if I got this going that isn't to say it's going to work within other apps. CUSTOM URI's (e.g. example://gizmos) are not clickable in other apps like Gmail and WhatsApp - so testing within the Android ecosystem is also problematic.

The answer at this stack overflow question is not acceptable since it does not answer the question but rather just encourages the use of the http:// version, I want the example:// scheme to work.

Siloam answered 24/12, 2015 at 9:53 Comment(1)
Please do not try to cram multiple questions into a single post. Even if you think they are related.Germayne
S
23

The ADB is sending the intent, it's just that your app must have separate intent-filters when you want multiple link types:

    <activity
        android:name="app.myActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Accepts URIs that begin with "example://gizmos”-->
            <data
                android:host="gizmos"
                android:scheme="example" />
        </intent-filter>
        <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="www.example.com"
                android:pathPrefix="/gizmos"
                android:scheme="http" />
            <!-- note that the leading "/" is required for pathPrefix-->                
        </intent-filter>
    </activity>

Then the following ADB commands both work:

adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos"
adb shell am start -W -a android.intent.action.VIEW -d "http://www.example.com/gizmos"

However I have encountered problems with more elements appended on to the custom URI (e.g. example://gizmos?data1='hello'&data2='world' - the '&' gets cut off)

And obviously this does not fix the fact that these links are not clickable in other apps, like WhatsApp and Gmail... and yet they are clickable on other platforms.

Siloam answered 24/12, 2015 at 9:53 Comment(4)
Thanks! This is SO not clear in Android's reference documentation!Airflow
I am sometimes amazed at how a company can be so renown but their documentation can be so obscure/misleading. This was very helpful however.Uranous
How to open the example://gizmos from the browser? The command adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos" is working fine.Tetrabrach
I think it depends on the browser - I know for example (from historical experience so may have changed) that WhatsApp just doesn't forward deep links - or rather, doesn't broadcast them on. You may want to look here for a potential solution: #35175555Siloam
W
7

As the other answer mentions, the android framework requires you to declare seperate intent-filters for each deep link that you enable in your app. That is the reason you're getting the error.

Also, instead of using adb, you can test deep links directly on android using deep link tester app:

https://play.google.com/store/apps/details?id=com.manoj.dlt

There is no need to mention any package name or component name. Just type the actual deep link and fire.

I've worked with deep links and personally, I found testing through adb to be time-consuming. Hence, I created this app for testing deep links directly and put it up on the play store.

Waiwaif answered 27/3, 2016 at 18:7 Comment(0)
T
1

There is DeepLinkDispatch library by airbnb. It will solve all your worries of handling DeepLink.

Library have feature like:

  • Easy to use
  • Handles all type of custom URL
  • Handle in link Parameters
  • Handles query parameters
Tam answered 28/2, 2020 at 4:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.