Working with HMS App Linking

Ekrem Hatipoglu
Huawei Developers
Published in
6 min readMay 11, 2020

Introduction

App Linking are links that work on Android platform and whether or not your app is already installed. With App Linking, developers can improve user experience in their apps. If a user opens a App Link on Android device , they can be taken directly to the linked content in your app. If a user opens the same App Link in a desktop browser, they can be taken to the equivalent content on your website.

You can use these links to direct users to promotional information or native app content that they can share with others. You can create links of App Linking and send them to users, or users can share links of App Linking dynamically generated in your app. Anyone who receives a link can tap it to access the linked content.

How does it work?

You can create a App Link many ways. You can create it by using the App Gallery Console, from app or manual way by adding parameters to a domain specific to your app.

When a user opens a link, if the app isn’t installed, the user is redirect to App Gallery to install your app else your app opens directly. You can then retrieve the link that was to your app and handle the deep link as do what you want for your app.

Creating a Link

Prerequisites

If you want to use the App Linking service, you must first have a developer account from AppGallery Connect. You need to create an application from your developer account and then integrate the HMS SDK into your project. I will not write these steps so that the article doesn’t lose its purpose and I will assume that it is already integrated in your project. You can find the guide from the link below.

HMS Integration Guide

Enable App Linking Service from AppGallery Connect

Go to Your App Project Settings > Growing > App Linking. On the App Linking page that is displayed, click Enable now. After that you will be ready for use App linking service.

Add the App linkking library to your project. build.gradle(App Level) -> dependencies

Add an URL prefix before create a link.

Go to URL Prefix Section > Click Add URL prefix.

URL prefix is free domain name provided by AppGallery Connect. You can add maksimum 5 url prefix.

You click after set domain name. Your url prefix will be ready.

Now you ready for create links. You can create links in 3 different ways. (AppGallery, Programatically and Manually)

AppGallery

Go to App Linking Section > Click Create App Linking

First step you should set short link. If you don’t have any URL prefix, firstly you should create a url prefix by click Add URL prefix

You can change the path after prefix url as you wish. The link you see in the link preview is our app link.

(https://ekrmh.dre.agconnect.link/OB48)

Second step you should set deep link. App Linking name field is your App Link name you see in the AppGallery Console. Deep link URL field is deep link url that your app handle it. We should handle that url for trigger something in our app.

Third step you should set Android link behavior. Actually we decide app link scenarios in this step. You can choose what will happen when user click the link or if user app’s version less than version number or users direct to AppGallery(optional). You can open an app or a website when click the link.

Fourth step is a optional step. If you want to track the effectiveness of campaigns across traffic sources. you should set UTM parameters for App Linking.

Final step is a another optinal step. If you want have better sharing experience, you should set sharing identifier.

You have completed all the steps. You can publish the app link by clickling Release.

Programmatically

You can create same App Link within your app.

You can create long app link like below.

Sample Long URL: https://ekrmh.dre.agconnect.link?region_id=1&deeplink=https%3A%2F%2Fekremh.com%2Ftest&android_package_name=com.ekrmh.safdet&android_min_version=10&campaign_name=HMS%20App%20Link%20Test&campaign_channel=HMS&campaign_medium=HMS%20App%20Link%20Test%20Medium&social_title=HMS%20App%20Link%20Title&social_image=https%3A%2F%2Fdeveloper.huawei.com%2FEnexport%2Fsites%2Fdefault%2Fimages%2Flogo.png&social_desc=HMS%20App%20Link%20Description

You can create short app link and test link like below.

Sample Short URL: https://ekrmh.dre.agconnect.link/8B48Sample Test URL:

https://ekrmh.dre.agconnect.link/8B48?test=1

You can convert long url to short url like below.

I gave the same url so the result will be the same.

Manually

You can find more information about create manually app link like below link.

Manually Constructing a Link of App Linking

You can create app link like this format.

https://PREFIX_URL.dre.agconnect.link/?deeplink=DEEP_LINK[&android_fallback_url=FALLBACK_URI][&android_min_version=APP_MIN_VERSION][&android_package_name=APP_PACKAGE_NAME][&social_desc=SOCIAL_DESCRIPTION][&social_image=SOCIAL_IMAGE_LINK][&social_title=SOCIAL_TITLE][&campaign_channel=UTM_SOURCE][&campaign_medium=UTM_MEDIUM][&campaign_name=UTM_CAMPAING]&region_id=REGION_ID

Sample Manual App Link:PREFIX_URL: ekrmh
DEEP_LINK: https://ekremh.com/test
FALLBACK_URI: -
APP_MIN_VERSION: 10
APP_PACKAGE_NAME: com.ekrmh.safder
SOCIAL_DESCRIPTION: HMS App Link Description
SOCIAL_IMAGE_LINK: https://developer.huawei.com/Enexport/sites/default/images/logo.png
SOCIAL_TITLE: HMS App Link Title
UTM_SOURCE: HMS
UTM_MEDIUM: HMS App Link Test Medium
UTM_CAMPAING: HMS App Link Test
REGION_ID: 1 (Europe)
https://ekrmh.dre.agconnect.link/?deeplink=https://ekremh.com/test&android_min_version=10&android_package_name=com.ekrmh.safder&social_desc=HMSAppLinkDescription&social_image=https://developer.huawei.com/Enexport/sites/default/images/logo.png&social_title=HMSAppLinkTitle&campaign_channel=HMS&campaign_medium=HMSAppLinkTestMedium&campaign_name=HMSAppLinkTest&region_id=1

Receiving Links From App

When a user click the an App Link, your app can call the API provided by the App Linking SDK to receive the clicked link and obtain data passed through the link.

Add IntentFilter tags to Activity in AndroidManifest.xml

android:host="DEEP_LINK_URL"

After that when user click the App link you can receive link and data like below. The deeplink in app link should start with ekremh.com otherwise AppLinkActivity won’t open.

Whitelist URL

You can only allow regular expression compatible urls to protect you from fishing attacks.

Go to Whitelist URL format Section > Click Add whitelist URL format

Whitelist URL format must be regular expressions.

You can find more detail about regular expressions below link.

Syntax

^https://ekremh\.com/.*$For this example, it can only allow urls starting with "https://ekremh.com", otherwise it will be blocked.

Debug View

You can see all the scenarios by clicking on the Debug.

Conclusion

In this article, you learned what is App Linking service and how to use it.

You can find more information about App Linking below the links.

Guide

Codelab

That is all.

Thanks.

--

--