Android deep links not following path prefix - android

The closest I could find was this question here. but it doesnt quite cover the problem im having.
I have deep links in my app setup to use /app as the path prefix. The problem I'm having is that links like http://example.com/upgrade are also trying to open in my app even though it doesn't have /app anywhere in the url. I understand you cant exclude urls specified by the prefix, but isn't the whole point of path prefix to include only those urls?
basically I want links like these to deep link:
http://example.com/app/home
http://example.com/app/specials
but not links like these:
http://exaple.com/
http://example.com/login
and here is what I have in my manifest:
<data android:scheme="http"
android:host="example.com"
android:pathPrefix="/app"/>
Edit 1
also found this link but i dont have any empty prefixes, only ones with just a slash "/"
Edit 2
The url that was triggering it was http://example.com/upgrade.php?app=1&method=1&uid=1, I wasn't sure if app after the ? would also trigger it so I changed the prefix to /application but that also didn't work, its still triggering them.
Edit 3
here are the other deep link data tags in the manifest:
profile activity
<data android:scheme="myapp"
android:host="profile"
android:pathPrefix="/"/>
login/signup activity
<data android:scheme="myapp"
android:host="login"
android:pathPrefix="/signup"/>
main activity
<data android:scheme="myapp"
android:host="main"
android:pathPrefix="/"/>
<data android:scheme="http"
android:host="test.example.com"
android:pathPrefix="/app"/>
<data android:scheme="http"
android:host="live.example.com"
android:pathPrefix="/app"/>
Edit 4
This is getting more and more confusing, if I remove the data tag with myapp as the scheme from the activity (or if i remove the pathPrefix from everything with a prefix of "/") it no longer triggers the deep links from the web urls, even if they have /app in them.

I figured it out, It seems like the data tags sort of bleed into one another, so the prefix of "/" on the data with scheme "example" was also applying to all of the schemes in the other data tags. I just had to use separate Intent filters for my custom scheme deep links and the url deep links like so:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- must start with http://test.example.com/app -->
<!-- http://test.example.com/ won't work since prefix / is in a different intent-filter -->
<data android:scheme="http"
android:host="test.example.com"
android:pathPrefix="/app"/>
<!-- must start with http://live.example.com/app -->
<data android:scheme="http"
android:host="live.example.com"
android:pathPrefix="/app"/>
</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" />
<!-- must start with example://main/ -->
<!-- http://test.example.com/ won't work since http is in a different intent-filter -->
<data android:scheme="example"
android:host="main"
android:pathPrefix="/"/>
</intent-filter>
</activity>

As explained here, your attributes get merged:
Although it's possible to include multiple <data> elements in the same filter, it's important that you create separate filters when your intention is to declare unique URLs (such as a specific combination of scheme and host), because multiple <data> elements in the same intent filter are actually merged together to account for all variations of their combined attributes.
Putting them into separate instances of <intent-filter> therefore fixes your problem.

Running a few tests on my own I think you have created this problem yourself, before continuing to test any more deep links from your app go to
Settings -> Your App Name -> Launch by Default and select Clear defaults
Then navigate back to your app manifest and add the intent filter to your Activity for each url:
In your example you ask for
http://example.com/app/home
http://example.com/app/specials
I am going to assume you have an Activity that would launch each one of these for the URI given above.
for HomeActivity in manifest do:
<activity android:name=".HomeActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- http://example.com/app/home -->
<data android:scheme="http"
android:host="example.com"
android:pathPrefix="/app/home"/>
<!-- https://example.com/app/home -->
<data android:scheme="https"
android:host="example.com"
android:pathPrefix="/app/home"/>
<!-- custom url scheme example://app/home -->
<data android:scheme="example"
android:host="app"
android:pathPrefix="/home"/>
</intent-filter>
</activity>
Test with adb shell am start -W -a android.intent.action.VIEW -d http://example.com/app/home and adb shell am start -W -a android.intent.action.VIEW -d example.com/app/home From your terminal
then for your SpecialsActivity
<activity android:name=".SpecialsActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- http://example.com/app/specials -->
<data android:scheme="http"
android:host="example.com"
android:pathPrefix="/app/specials"/>
<!-- https://example.com/app/specials -->
<data android:scheme="https"
android:host="example.com"
android:pathPrefix="/app/specials"/>
<!-- custom url scheme example://app/specials -->
<data android:scheme="example"
android:host="app"
android:pathPrefix="/specials"/>
</intent-filter>
</activity>
Test with adb shell am start -W -a android.intent.action.VIEW -d http://example.com/app/specials and adb shell am start -W -a android.intent.action.VIEW -d example.com/app/specials From your terminal
this is pretty much how it works, if you set your app to handle a http://example.com/ url and select choose by default then, well its going to try to open http://example.com/{anything} using your app
So in short, when you specify your pathPrefix, make sure you include what you really want to intercept and don't choose your app by default if you test a link that shouldn't open your app, just adjust your pathPrefix so it won't be triggered by your app.
For example the URLS above won't open
http://example.com/app
because we haven't defined a <data /> tag to handle this.
UPDATE
And to add straight from the docs when using \ or *:
Because '\' is used as an escape character when the string is read from XML (before it is parsed as a pattern), you will need to double-escape: For example, a literal '*' would be written as '\\*' and a literal '\' would be written as '\\\\'. This is basically the same as what you would need to write if constructing the string in Java code.
Good luck and happy coding!

Related

Registering Android deep link path without including subdomains

I am trying to create a deep link to match a URL specifically on the specified domain, and not on any subdomains. My intent filter <data /> entry looks like this:
<data
android:host="example.com"
android:pathPrefix="/somepath"
android:scheme="https" />
This works fine, and matches URLs with https://example.com/somepath as I'd expect. However, there are also URLs that I have that look like https://subdomain.example.com/somepath that I do not want to match. These are picked up by that data entry as well!
I want to match STRICTLY on the host and not include any subdomains. Is this possible in Android?
It's not possible to change the URL scheme for either set of links.
As I see in the documentation, your configuration will not match with subdomains.
If you want to accept all subdomains you have to use an asterisk (*) character before your host like this: *.example.com.
So, the answer is: example.com will not match with any subdomains like subdomain.example.com
I have created and Activity with the following 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="example.com"
android:pathPrefix="/somepath"
android:scheme="https" />
</intent-filter>
This opens test.example.com in a browser window:
adb shell am start -a android.intent.action.VIEW -d "https://test.example.com/somepath/test"
This opens my application:
adb shell am start -a android.intent.action.VIEW -d "https://example.com/somepath/test"

Launching an App from a URL

I am attempting to get the Android application chooser to launch when a specific URL is clicked from within a registration e-mail. I have already looked at the below question and several others but I am still having no luck with this.
Launching Android Application from link or email
I have created my intent in the AndroidManifest.xml file as below (I've placed my website address where you see ".website.org"):
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</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:scheme="https"
android:host="*.website.org"
android:path="/" />
</intent-filter>
</activity>
Is anyone able to assist me with what else I may be missing, as this is currently not launching anything and it's just loading the link directly in the default web browser?
You achieve this by Deep linking in your app.
First of all you need to add intent filters for incoming links.
<action>
Specify the ACTION_VIEW intent action so that the intent filter can be
reached from Google Search.
<data>
Add one or more tags, each of which represents a URI format
that resolves to the activity. At minimum, the tag must include
the android:scheme attribute.
You can add more attributes to further
refine the type of URI that the activity accepts. For example, you
might have multiple activities that accept similar URIs, but which
differ simply based on the path name. In this case, use the
android:path attribute or its pathPattern or pathPrefix variants to
differentiate which activity the system should open for different URI
paths.
<category>
Include the BROWSABLE category. It is required in order for the intent
filter to be accessible from a web browser. Without it, clicking a
link in a browser cannot resolve to your app.
Also include the DEFAULT
category. This allows your app to respond to implicit intents. Without
this, the activity can be started only if the intent specifies your
app component name.
I have used this url for launch my app “http://www.example.com/gizmos”
Look at the my Manifest.xml file,
<activity
android:name="com.example.android.GizmosActivity"
android:label="#string/title_gizmos" >
<intent-filter android:label="#string/filter_view_http_gizmos">
<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 "http://www.example.com/gizmos” -->
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/gizmos" />
<!-- note that the leading "/" is required for pathPrefix-->
</intent-filter>
<intent-filter android:label="#string/filter_view_example_gizmos">
<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:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
And notice that the two intent filters only differ by the <data> element.
<intent-filter>
...
<data android:scheme="https" android:host="www.example.com" />
<data android:scheme="app" android:host="open.my.app" />
</intent-filter>
It might seem as though this supports only https://www.example.com and
app://open.my.app. However, it actually supports those two, plus
these: app://www.example.com and https://open.my.app.
Read data from incoming intents
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}
Test your deep links
The general syntax for testing an intent filter URI with adb is:
$ adb shell am start
-W -a android.intent.action.VIEW
-d <URI> <PACKAGE>
For example, the command below tries to view a target app activity that is associated with the specified URI.
$ adb shell am start
-W -a android.intent.action.VIEW
-d "example://gizmos" com.example.android
Thanks for your replies. In the end I had to change my intent to the below and it had the desired result. When I click the URL specified in my intent the application chooser pops up allowing me to load the link in my app.
Just as info for anyone struggling as I did, you do not need to read data from incoming intents for the application chooser to launch, all you need is the below in your AndroidManifest file.
<intent-filter android:autoVerify="true">
<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" />
<data android:scheme="https" />
<data android:host=".website.com" />
</intent-filter>
Thanks All.
First of All try to write the whole path for the Url in
android:host"https://www.google.com"
And next please check that you have not opted for default browser for link in android device.
Remove any default for link if any and try again.
Android studio made this task very easy they introduced App Links Assistant in Android Studio 2.3 -. It does every thing for you and test as well.
It can be found in android studio
Tools > App Links Assistant.
useful demo video can be seen at this link.
https://www.youtube.com/watch?v=lpaByLW_ctw

Android app indexing - deep linking for urls with GET params like www.abc.com?parameter=value

I'm creating a intent-filter in order to filter in my app urls like https://www.hotelsclick.com/?hotel_id=135738
I see in the documentation that I should create an intent filter like
<intent-filter android:label="#string/filter_title_viewgizmos">
<action android:name="android.intent.action.VIEW" />
<!-- Accepts URIs that begin with "http://example.com/gizmos” -->
<data android:scheme="http"
android:host="example.com"
android:pathPrefix="/gizmos" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
This intent-filter should filter URLs like "http://example.com/gizmos?1234, http://example.com/gizmos/1234, http://example.com/gizmos/toys/1234"
That's good but... my URL is different, it's like http://example.com?toys=1234 'cause it's got a named GET parameter just in the home page, which is hotel_id.
How can I filter such URLs? Is there some more parameter to put in the intent-filter definition?
Edit: I put the following intent-filter in my Manifest.xml
<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="https"
android:host="hotelsclick.com" />
<data android:scheme="http"
android:host="hotelsclick.com" />
</intent-filter>
and I CAN open the app by providing this ADB command
adb shell am start -a android.intent.action.VIEW -d "http://hotelsclick.com?hotel_id=135738" com.towers.hotelsclick
BUT it doesn't work from the page self-generated with the deep-link tool: https://developers.google.com/app-indexing/webmasters/test?hl=it
I put in the webpage the following URI in the editText: "android-app://com.towers.hotelsclick/hotelsclick.com?hotel_id=135738" and I got this page: https://applinktest.appspot.com/app-link.html?url=android-app%3A%2F%2Fcom.towers.hotelsclick%2Fhotelsclick.com%3Fhotel_id%3D135738
As you can see the link in the page is intent://#Intent;scheme=hotelsclick.com?hotel_id=135738;package=com.towers.hotelsclick;end and I expected this intent-link to be opened by the app itself when clicked on mobile on the same phone I used for the adb command before. But it doesn't work and takes me straight to the google play store, suggesting to open the app from there
So, since I can make the intent-filter work via ADB but not with a proper link, can I say I succeeded or not?
Thank you
The generated link is not valid. It should be
intent://hotelsclick.com?hotel_id=135738#Intent;scheme=http;package=com.towers.hotelsclick;end
instead of
intent://#Intent;scheme=hotelsclick.com?hotel_id=135738;package=com.towers.hotelsclick;end

Deeplinking to an android app

Can we implement deeplinking for a mobile app without having a website or do we have to have a predefined specific website to implemment it?
Deep linking can work without having a specific website. All you need for it to work is add the <intent-filter> ... </intent-filter> tag to your preferred activity via the AndroidManifest.
Then create a unique URI pattern intended to match any HTTP URI such as http://your-app-domain/content or a custom scheme such as your-app://content. Add this scheme to the <data ... /> tag.
Here's what your activity should look like when you're done.
<activity
android:name=".activities.DeepLinkingActivity"
android:label="#string/title_activity_deeplink">
<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="your-app-domain"
android:pathPrefix="/content"
android:scheme="http" />
</intent-filter>
</activity>
Regardless of the URI scheme you choose, you should be able to begin your activity when an intent with the scheme is triggered.
If you are familiar with adb commands, enter this command
adb shell am start -a android.intent.action.VIEW -d "http://your-app-domain/content" com.your-app-package
Further reading https://developers.google.com/app-indexing/android/app

What is the specifier for empty host in intent-filter?

I'm trying to make my app respond to these schemes:
my-app://product/XXX
and
my-app://
The following code works, but I'm getting warning that android:host cannot be empty. It still works, but - is there a correct way to specify empty host?
I don't want to specify "*" as android-host, as there are other activities that handle different actions, and then they don't open directly, but I'm getting a chooser dialog to select which activity should open.
<activity android:name=".ui.OpenerActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait" >
<intent-filter>
<data android:scheme="my-app" />
<data android:host="product" />
<data android:host="" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
Thanks a lot!
Not having a host (or, more formally authority) part in you URI ist absolutely fine. Think about the commonly used file:///tmp/example.file
Note though that a hostless, hierarchical URI is not opaque (like mailto:john#example.com), but has a path. Like the file-URI the hostname gets replaced by a single /.
Your URI should therefore be of the form my-app:///product/XXX. The Intent Filter could look like this:
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<data android:host=""/>
<data android:scheme="my-app"/>
<data android:pathPrefix="/product"/>
<data android:pathPattern=".+"/>
</intent-filter>
You will still get the warning about the empty hostname, which is absolutely ok. This warning is the result of a check for Firebase App Indexing, which I guess only makes sense in the presence of a website (from which you would take the hostname, ie. whole URIs then).
Since you will not use the indexing anyway, you can remove the warning by adding the following to your project's lint.xml:
<!-- no app indexing without a hostname -->
<issue id="GoogleAppIndexingUrlError" severity="ignore"/>
For more details, looks at the URI spec and Wikipedia entry on file-URIs.

Categories

Resources