Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling specific routes in Android M app links

Android M has added support for App links by creating a special assetlinks.json on the web server to delegate link handling to a mobile app.

For example, here's the content of such file:

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.costingtons.app",
    "sha256_cert_fingerprints": ["14:6D:E9:83:C5:73:06:50:D8:EE:B9:95:2F:34:FC:64:16:"
     "A0:83:42:E6:1D:BE:A8:8A:04:96:B2:3F:CF:44:E5"]
  }
}]

Currently, the relation is defined as common.handle_all_urls which means any url will be routed to the app.

I would like the app to open specific routes, for example https://costingtons.com/products/a-product would open in the app but https://costingtons.com would open the web version.

Apple Universal Links similar configuration file can define paths to be opened in the app, and the syntax supports pattern matching like /products/* etc.

Is there any way to do something like that with App Links in Android ?

like image 922
Michael Avatar asked Mar 07 '16 09:03

Michael


2 Answers

The assetlinks.json file specifies that that particular app is allowed to open all URLs, but the app itself (via the intent filter in its AndroidManifest) can be more restrictive so that it only opens the product links, and not the general website link.

Furthermore even if the intent filter is invoked for a particular URL, the receiver can still decide not to handle it, leaving it to the next handler (often the browser). (Maybe you want the app to handle links when on cellular, but let the browser handle them when on wifi).

like image 164
zmarties Avatar answered Oct 22 '22 09:10

zmarties


As far as I can tell, there's no way to set up the assetlinks file to determine which paths can/should open the app (the way that you can with the iOS AASA file).

However, since Android employs Intent Filters, you can use those to restrict which paths should open the app. Per an earlier comment, the assetlinks file only says which URLs can open the app. It's up to the app to define which URLs should open the app.

So with your above example, you'd need to define an Intent Filter for one of your activities which would look something like:

<intent-filter android:autoVerify="true">
    <category android:name="android.intent.category.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <!-- Only accept URLs that look like https://costingtons.com/products/* -->
    <data
        android:scheme="https"
        android:host="costingtons.com"
        android:pathPrefix="/products/" />
</intent-filter>

The biggest limitation that I've found with this is that there's no way to exclude certain paths (i.e. NOT URLs with a prefix of xyz), so you'd have to come up with an exhaustive set of Intent Filters matching all URLs that you DO want to allow into the app.

like image 41
themarshal Avatar answered Oct 22 '22 09:10

themarshal