Deep Linking
You may set up a deep link for your preferred URL so that when users taps the defined URL from anywhere outside your app in their Android device, they will be redirected to your app for a smoother experience.
In this example, we will explain how to set up deep linking to open TapTalk.io's room list or a specific chat room page when user taps a specific link from their device.
Step 1: Define a URL to open your app
To start, choose a URL from your domain that will be used to open your app. In this example, we will try using https://web.taptalk.io/chat.
You should use your own specified URL, the URL above is only meant as an example to make it easier to follow the guide.
Step 2: Specify an Activity to handle Deep Link
To be able to open your app from a URL, you will need to add an intent filter to your specified activity's tag inside the AndroidManifest.xml file. In this example, a new activity named TapDeepLinkActivity will be created specifically to handle deep links. You may also use an existing activity, such as your app's main activity, if you wish.
After you locate your chosen activity tag in the Manifest, add the following intent filter inside the <activity>
tag. The previously defined URL as will be added as data for the intent filter.
From Android Developer:
To create a link to your app content, add an intent filter that contains these elements and attribute values in your manifest:
• <action>
Specify the ACTION_VIEW
intent action so that the intent filter can be reached from Google Search.
• <data>
Add one or more <data>
tags, each of which represents a URI format that resolves to the activity. At minimum, the <data>
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.
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.
Step 3: Opening a Specific Activity from Deep Link
The TapDeepLinkActivity from this example will act as a bridge to open TapTalk.io's room list page, or a specified chat room.
When the activity is launched from an intent filter, you can obtain the required data from the incoming Intent from the Activity.
The example snippet below will show how to open a chat room from the specified roomID from the URL (i.e. https://web.taptalk.io/chat/roomID), or open the room list page when no roomID is specified
You can also try using the above snippet for your own preferred Activity.
Step 4: Handling Deep Link for Android 12+
Starting in Android 12 (API level 31), a generic web intent resolves to an activity in your app only if your app is approved for the specific domain contained in that web intent. If your app isn't approved for the domain, the web intent resolves to the user's default browser app instead.
To handle this change, we will need to create an assetlinks.json file and place it under the .well-known/ directory of your host.
Creating the assetlinks.json file
You may create the file manually, create an empty file and name it assetlinks.json. The file should contain the following:
The above example shows the contents of assetlinks.json file addressing your application for both debug and release build. Change the package_name
and sha256_cert_fingerprints
value respectively according to your application.
package_name
can be obtained from the applicationId
in your build.grade (:app) file.
One way to obtain SHA-256 fingerprint for your app is through running gradle signingReport
from Android Studio.
Click on the gear icon on the top right of Android Studio, then select Run Anything, and type gradle signingReport, then press Enter. (menu location may vary according to version)
Android Studio will then execute a signingReport task. When it is finished, you can see all your app variants' fingerprints in the Debug tab at the bottom of the Android Studio window. Paste the SHA-256 value of your selected app variant to the assetlinks.json file.
Deploying the assetlinks.json file
After generating the file, deploy it to the host under a .well_known directory. For this specific example, the file will be located at https://web.taptalk.io/.well-known/assetlinks.json
Google provides some useful information to watch out for when hosting this file:
The assetlinks.json file must be accessible without any redirects (no 301 or 302 redirects) and must be accessible by bots (your robots.txt must allow crawling /.well-known/assetlinks.json).
The assetlinks.json file must be served with content-type application/json.
The assetlinks.json file must be accessible over an HTTPS connection, regardless of whether the app's intent filters declare HTTPS as the data scheme.
Once all the steps above are successfully done, your app should be approved for your domain, and should be able to handle deep linking. You can try tapping the specified url from a test device to open your app.
Last updated