简体   繁体   中英

Open app with (deep-) link containing path and query-parameter

These three urls are given:

1) https://example.com

2) https://example.com/app

3) https://example.com/app?param=hello

Assumed that i receive a mail in the gmail-app with these three links, I need the following behaviour:

1) Should not open the app

2) Should open the app

3) Should open the app and extract the parameter's value

What i have achieved so far:

    <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="example.com"
            android:pathPrefix="/app"
            android:scheme="https" />
    </intent-filter>

This snippet is working fine for the cases 1) and 2) : The first url is not opened in the app, the second one is. But the third link sadly i not opened by the app.

I also tried some different variations with path , pathPrefix and pathPattern , but i had no luck achieving all three given behaviours.

So I need your help guys, can you provide a snippet that meets the given requirements or some hints I can test?

Update:

Changing android:pathPrefix to android:pathPattern works now fine: The system's intent chooser is only shown for cases 2) and 3) , case 1) opens the browser directly.

but

what I want to achieve additionally is to check for a specific parameter before entering the app or triggering the intent chooser. This should only happen when the parameter param holds the value hello and not goodbye . Is this possible with some kind of regular expression inside the pathPattern -attribute?

I hope this solution help you in solving the task.

Manifest.xml

Don't include android:pathPrefix="/app" in Manifest.xml

<activity android:name=".YourActivity">
        <intent-filter android:label="@string/app_name">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="http"
                  android:host="example.com"/>
        </intent-filter>
</activity>

In YourActivity.kt check the Intent data to perform further action.

Note: The code is written in Kotlin

    val action = intent.action
    val data = intent.dataString
    if (Intent.ACTION_VIEW == action && data != null) {
        if (data.equals("http://example.com")) {
            Toast.makeText(this, "contains only URL", Toast.LENGTH_SHORT).show()
        } else if (data.contains("http://example.com/") && !data.contains("?")) {
            Toast.makeText(this, "contains URL with pathPrefix", Toast.LENGTH_SHORT).show()
        } else if (data.contains("http://example.com/") && data.contains("?")) {
            Toast.makeText(this, "contains URL with data", Toast.LENGTH_SHORT).show()
        }
    } else {
        Toast.makeText(this, "Intent from Activity", Toast.LENGTH_SHORT).show()
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM