Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Deep Linking with custom URI

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.

like image 769
Quintin Balsdon Avatar asked Dec 24 '15 09:12

Quintin Balsdon


People also ask

How to create deep links to app content in Android?

Create Deep Links to App Content. When a clicked link or programmatic request invokes a web URI intent, the Android system tries each of the following actions, in sequential order, until the request succeeds: Open the user's preferred app that can handle the URI, if one is designated. Open the only available app that can handle the URI.

How to test intent filter URIs for deep linking?

You can use the Android Debug Bridge with the activity manager (am) tool to test that the intent filter URIs you specified for deep linking resolve to the correct app activity. You can run the adb command against a device or an emulator.

How do I test a deep link on Android?

Test your deep links. You can use the Android Debug Bridge with the activity manager (am) tool to test that the intent filter URIs you specified for deep linking resolve to the correct app activity. You can run the adb command against a device or an emulator.

How do I create a deep link in Android navcontroller?

val componentName = ... If you have an existing NavController , you can also create a deep link by using NavController.createDeepLink (). An implicit deep link refers to a specific destination in an app. When the deep link is invoked—for example, when a user clicks a link—Android can then open your app to the corresponding destination.


2 Answers

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.

like image 78
Quintin Balsdon Avatar answered Sep 30 '22 16:09

Quintin Balsdon


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
like image 25
Sumit Jain Avatar answered Sep 30 '22 17:09

Sumit Jain