[Huawei Developers] muraliameakula:
Introduction

In this article, we will learn how to implement Huawei App Linking service. This service is very important service for enterprises that are digitalizing to effectively manage deep links.

What can we do using App Linking?

Huawei App Linking is a very useful service to make existing deep links smarter and useful. Developers can improve user experience in their apps. If user opens the link android or iOS it can be directly forwarded to the linked content in your application.

We can use these links to direct users to promotional information or native content that they can share with others. We can create links of app linking and send them to users or users share links of app linking dynamically generated in application. User can click the link to access the content.

Do you know how it will work?

Developer can create links in different ways using App Gallery console, from app or manual by adding required parameters to a specific domain for app.

Users clicks a link if the app is not installed, the user is redirect to App Gallery to install your app otherwise your app opens directly. You can retrieve the link that was to your app and handle the deep link as per requirement of your app.

Table of content

1. Project setup

2. Create Link using App Gallery console

3. Create Link from APP

Mobile app Linking Benefits

1. Enhance the user experience: users can easily access the linked content, with essentially no navigation. If you are linking to something in your app from social media, a mobile website, etc., users are able to navigate seamlessly to that content.

2. Improve User Retention, Engagement, and Usage: Users who were deep linked showed double the activation rate, double the retention rate, and visited the app twice as frequently versus users who had not been deep linked.

3. Help Re-Engage users: When a user has your app installed, but has been inactive for a period of time, you can use deep linking to direct them to specific content to encourage use, rather than the generic home screen.

Requirements

1. Any operating system (i.e. MacOS, Linux and Windows).

2. Any IDE with Flutter SDK installed (i.e. IntelliJ, Android Studio and VsCode etc.).

3. A little knowledge of Dart and Flutter.

4. Minimum API Level 19 is required.

5. Required EMUI 5.0 and later version devices.

Setting up the APP Linking

1. First create a developer account in AppGallery Connect. After create your developer account, you can create a new project and new app. For more information, click here.

2. Generating a Signing certificate fingerprint follow below command.

keytool -genkey -keystore <application_project_dir>\android\app\<signing_certificate_fingerprint_filename>.jks -storepass <store_password> -alias <alias> -keypass <key_password> -keysize 2048 -keyalg RSA -validity 36500

3. The above command creates the keystore file in appdir/android/app.

4. Now we need to obtain the SHA256 key, follow the command.

keytool -list -v -keystore <application_project_dir>\android\app\<signing_certificate_fingerprint_filename>.jks

5. Enable the App Linking service on the App Gallery.

6. After configuring project, we need to download agconnect-services.json file in your project and add into your project.

​

7. After that follow the URL for cross-platform plugins add required plugin into sample.

8. The following dependencies for HMS usage need to be added to the build.gradle file under the android directory.

buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'}
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'}

}
}

9. Add the below plugin into build.gradle file under the android/app directory.

apply plugin: 'com.huawei.agconnect'

10. Add the required permissions to the AndroidManifest.xml file under app/src/main folder.

<uses-permission android:name="com.huawei.permission.SECURITY_DIAGNOSE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.INTERNET" />

<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="developer.huawei.com"
android:scheme="http"/>
<data
android:host="developer.huawei.com"
android:scheme="https"/>
</intent-filter>

11. After completing all the above steps, you need to add the required kits’. Flutter plugins as dependencie to pubspec.yaml file. You can find all the plugins in pub.dev with the latest versions.

agconnect_applinking: ^1.2.0+201

12. To launch the url we need to add flutter url_launcher plugin.

url_launcher: ^5.7.10

After adding them, run flutter pub get command. Now all the plugins are ready to use.

Note: Set multiDexEnabled to true in the android/app directory, so the app will not crash.

Config App Linking

1. We can access the App Linking on the console side by following the My Projects > Project settings > Grow > App Linking steps.

2. Before creating our custom link we need to add URL prefix. URL Prefix are free domains provided by AppGallery Connect. If you do not have a custom domain address. We can create up to 5 URL prefixes.

Enter domain name and then click on Next button.

3. After creating the URL Prefix we can create our links after following the App Linking Tab > Create App Linking steps.

4. We need to set a short link value to the URL prefix we created. Short link value is generated automatically by Console. However, if we wish, we can arrange this area according to our wishes.

5. After setting up all the necessary information, now we will define the behavior of our deep link, by which we can determine how our deep links will behave.

6. We need to enter preview information such as title, image, description this information optional. Click Next and then click Release.

Creating Link

Now let’s create the same link within the application.create object for AGCAppLinking.

To Generate LongLinks

final AGCAppLinking agcAppLinking = new AGCAppLinking();
createLongAppLinking(BuildContext context) async {
AndroidLinkInfo androidLinkInfo = new AndroidLinkInfo(
androidOpenType:
AppLinkingAndroidLinkInfoAndroidOpenTypeConstants.APP_GALLERY,
androidPackageName: "com.huawei.sample.wellfit",
androidDeepLink:
'https://appgallery.huawei.com/#/app/C101529369');

ApplinkingInfo appLinkingInfo = ApplinkingInfo(
androidLinkInfo: androidLinkInfo,
shortAppLinkingLength: ShortAppLinkingLengthConstants.SHORT,
domainUriPrefix: 'https://wellfit.dra.agconnect.link',
deepLink: 'https://appgallery.huawei.com/#/app/C101529369',
previewType: AppLinkingLinkingPreviewTypeConstants.APP_INFO);

try {
setState(() async {
longAppLinking =
await agcAppLinking.buildLongAppLinking(appLinkingInfo);
print(longAppLinking.longLink.toString());
});
} on PlatformException catch (e) {
_showDialog(context, e.toString());
}
}

To Generate ShortLink

try {
ShortAppLinking shortAppLinking =
await agcAppLinking.buildShortAppLinking(appLinkingInfo);
print(shortAppLinking.toString());
} on PlatformException catch (e) {
_showDialog(context, e.toString());
}

Demo

Tips and Tricks

1. Download latest HMS Flutter plugin.

2. Set minSDK version to 19 or later.

3. Do not forget to click pug get after adding dependencies.

4. HMS Core APK 4.0.2.300 is required.

5. Currently this service supports 5 URLs.

Conclusion

That’s it!

We have finished a complete demo of a flutter app that app handles Huawei App Linking services. This service can bring significant improvements to the user experience of our mobile apps.

Thanks for reading! If you enjoyed this story, please click the Like button and Follow. Feel free to leave a Comment 💬 below.

Reference

App Linking service URL

Original Source