3

My app handles deep links. And when I try to retrieve my deep link URL, I do get not the full URL (without query params).

My AndroidManifest:

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

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

                <data
                    android:host="www.test.com"
                    android:scheme="testlink" />
            </intent-filter>

For example, there is a deep link which I need to handle: testlink://www.test.com/strategy?year=2021

And inside activity I'm trying to get data:

@Override
protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
    String url = getIntent().getData().toString();
 
}

And I get a URL like this: testlink://www.test.com/strategy? but expected testlink://www.test.com/strategy?year=2021 . Query params are clipped.

Why might this be happening? Please, help me.

NOTE: with HTTPS or HTTP scheme I get the URL from intent as expected, but when I use custom scheme query params are lost

testivanivan
  • 967
  • 13
  • 36

2 Answers2

4

Include the following in your activity's intent filter in your application manifest:

            <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.test.com"
                    android:scheme="testlink" />
            </intent-filter>

Then in your onCreate function in that activity you can use:

Uri uri = this.getIntent().getData();

to retrieve the android.net.Uri from the intent.

If you want more information from the Uri you can use:

//To get the full url passed in use
uri.toString();

//To get the data after the '?' only use
uri.getEncodedQuery();

From here: https://stackoverflow.com/a/58346741/2396026

Above code tested and working on the API30 x86 emulator image.

allnewryan
  • 66
  • 7
1

You must split your Uri by this character '?' to get year=2021 like this.

Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();

if (Intent.ACTION_VIEW.equals(appLinkAction) && appLinkData != null) {
    String lastUrlId = appLinkData.getLastPathSegment(); // strategy?year=2021
    String[] segments = lastUrlId.split("?");
    ....
}