[Huawei Developers] ask011:
Introduction

In-App linking is refer to App linking or deep linking in general, however it is particularly for the use case of sharing content with the same application for the same application installed on the android device.

Huawei App Linking leverage developers/users to create cross platform links which can work as defined and can be distributed with multiple channels to users.

When the user clicks the link, it will be re-directed to the specified in-app content.

Huawei App Linking has multiple functions:

1. Support for deferred deep links

2. Link display in card form

3. Data statistics

Huawei App Linking supports the link creation in multiple ways:

1. Creating a link in AppGallery Connect

2. Creating a link by calling APIs on the client

3. Manually constructing a link

Creating a link in AppGallery Connect

We will be focusing on the Huawei App linking capabilities to create the deep links for our Android application through Huawei AppGallery Connect.

Use Case

There could be multiple use cases for this capability, however I will throw some light on a use case which is most commonly adapted by the applications with complex UX and high transmission between different in-app pages.

Development Overview

1. Must have a Huawei Developer Account

2. Must have Android Studio 3.0 or later

3. Must have a Huawei phone with HMS Core 4.0.2.300 or later

4. EMUI 3.0 or later

Software Requirements

1. Java SDK 1.7 or later

2. Android 5.0 or later

Preparation

1. Create an app or project in Android Studio.

2. Create an app and project in the Huawei AppGallery Connect.

3. Provide the SHA Key and App Package name of the project for which you want the App Linking to be done (Example: News application)

4. Download the agconnect-services.json file and paste to the app folder of your android studio.

Integration

Add below to build.gradle (project)file, under buildscript/repositories and allprojects/repositories.

Maven {url 'http://developer.huawei.com/repo/'}

Add below to build.gradle (app) file, under dependencies to use the App Linking SDK.

dependencies{
// Import the SDK.
implementation 'com.huawei.agconnect:agconnect-applinking:1.4.1.300'
}

News Application

News application is developed with multiple content and complex UX to show the capability of the Huawei App Linking.

I will highlight some if the important code blocks for this application.

Main Activity

This activity is the entry point for the application which will have the navigation tab to handle the multiple news categories.

Also, it receives the App Link, read it and re-direct it to the corresponding content.

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private ViewPager viewPager;
FloatingActionButton AddLinks;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

// Find the view pager that will allow the user to swipe between fragments
viewPager = findViewById(R.id.viewpager);

// Give the TabLayout the ViewPager
TabLayout tabLayout = findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
// Set gravity for tab bar
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

NavigationView navigationView = findViewById(R.id.nav_view);
assert navigationView != null;
navigationView.setNavigationItemSelectedListener(this);

// Set the default fragment when starting the app onNavigationItemSelected(navigationView.getMenu().getItem(0).setChecked(true));

// Set category fragment pager adapter
CategoryFragmentPagerAdapter pagerAdapter =
new CategoryFragmentPagerAdapter(this, getSupportFragmentManager());
// Set the pager adapter onto the view pager
viewPager.setAdapter(pagerAdapter);

AddLinks.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)

shareLink("https://bulletin.dra.agconnect.link/yAvD")
}
});

//Receive and re-direct app link
recievelink();

}

//Receive applinks here
private void recievelink()
{ AGConnectAppLinking.getInstance().getAppLinking(getIntent()).addOnSuccessListener(new OnSuccessListener<ResolvedLinkData>() {
@Override
public void onSuccess(ResolvedLinkData resolvedLinkData) {

Uri deepLink1 = null;
if (resolvedLinkData != null) {
deepLink1 = resolvedLinkData.getDeepLink();
String myLink= deepLink1.toString();
if(myLink.contains("science")) {
viewPager.setCurrentItem(Constants.SCIENCE);
}
try {
Toast.makeText(MainActivity.this, deepLink1.toString(), Toast.LENGTH_SHORT).show();
}catch (Exception e)
{
e.printStackTrace();
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e("LOG ", "Exception Occured : " + e.getMessage());
e.printStackTrace();
ApiException apiException = (ApiException) e;
Log.e("LOG ", "status code " + apiException.getStatusCode());
}
});
}

private void shareLink(String appLinking) {
if (appLinking != null) {
System.out.println("inside button click " + appLinking);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, appLinking);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

}
}

@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

// Switch Fragments in a ViewPager on clicking items in Navigation Drawer
if (id == R.id.nav_home) {
viewPager.setCurrentItem(Constants.HOME);
} else if (id == R.id.nav_world) {
viewPager.setCurrentItem(Constants.WORLD);
} else if (id == R.id.nav_science) {
viewPager.setCurrentItem(Constants.SCIENCE);
} else if (id == R.id.nav_sport) {
viewPager.setCurrentItem(Constants.SPORT);
} else if (id == R.id.nav_environment) {
viewPager.setCurrentItem(Constants.ENVIRONMENT);
} else if (id == R.id.nav_society) {
viewPager.setCurrentItem(Constants.SOCIETY);
} else if (id == R.id.nav_fashion) {
viewPager.setCurrentItem(Constants.FASHION);
} else if (id == R.id.nav_business) {
viewPager.setCurrentItem(Constants.BUSINESS);
} else if (id == R.id.nav_culture) {
viewPager.setCurrentItem(Constants.CULTURE);
}

DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

@Override
// Initialize the contents of the Activity's options menu
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the Options Menu we specified in XML
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
// This method is called whenever an item in the options menu is selected.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent settingsIntent = new Intent(this, SettingsActivity.class);
startActivity(settingsIntent);
return true;
}
return super.onOptionsItemSelected(item);
}

}

Creating a link in AppGallery Connect to directly open the Science tab for the News Application through a link

1. Login to AppGallery Connect.

2. Choose My Projects > NewsWorld(App Linking).

3. Go to>Growing > App Linking>Enable now

4. Once you enable the app linking for your project by setting up the country and region choose URL prefix>Add URL prefix

Add URL prefix, which is a free domain name provided by AppGallery Connect with a string.

Tip: URL prefix should be unique and can contain only lowercase letters and digits.

Select Set domain name option and then click on Next button.

The following page will be displayed.

6. Click App Linking and then click Create App Linking for deep linking purpose. It is required to directly navigate to specific in-app content from a different application.

7. It will suggest short link by itself or you can customise it as shown below.

8. Click on Next button and set the deep link.

App Linking name: deep link name.

Default deep link: deep link used to open an app.

Android deep link: deep link preferentially opened on an Android device.

iOS deep Link URL: deep link preferentially opened on an iOS device.

Tip 1: Link name and Default deep link can be same and follow as https://domainname.com/xxx

Where “domainname” is URLprefix which we set above and “xxx” is specific in-app content page to re-direct.

Tip 2: You can customize the deep link, which is different from the URL prefix of the link.

Once done, click on Next button.

9. Select Set Android link behaviour for your Android application as below.

We will choose “Open in app” as we want our application to open directly as application.

Select your application from the Add Android app menu.

Redirect user to AppGallery page if the application is not installed on the device.

Tip: Ensure that the App Store ID and team ID have been configured. If they are not configured, add them as prompted.

Once done, click on Next button.

10. We have set the deep link for our news application and it will re-direct to in-app content tab (science) every time when it is shared from our share application.

To check the details and use the link we will navigate to view details

11. We will use short App link for our App Linking use case. It will re-direct the user to Science tab of news application from our shared link through different tab.

Receiving Links of App Linking

When a user is directed to the target content after tapping a received link of App Linking, your app can call the API provided by the App Linking SDK to receive the tapped link and obtain data passed through the link.

Add an Intent Filter to manifest file

Add intent filter to activity which will receive and process the link.

We will add this for Main activity for our use case.

<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="yourdomainname.com"
android:scheme="https"
tools:ignore="AppLinkUrlError" />
</intent-filter>

Receive the link in our Main activity

Add below code to your activity to receive and process the link.

AGConnectAppLinking.getInstance().getAppLinking(getIntent()).addOnSuccessListener(new OnSuccessListener<ResolvedLinkData>() {
@Override
public void onSuccess(ResolvedLinkData resolvedLinkData) {

System.out.println("inside button click_rcv " + resolvedLinkData.getDeepLink());
Uri deepLink1 = null;
if (resolvedLinkData != null) {
deepLink1 = resolvedLinkData.getDeepLink();
String myLink= deepLink1.toString();
if(myLink.contains("science")) {
viewPager.setCurrentItem(Constants.SCIENCE);
}
try {
Toast.makeText(MainActivity.this, deepLink1.toString(), Toast.LENGTH_SHORT).show();
}catch (Exception e)
{
e.printStackTrace();
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e("LOG", "Exception Occured : " + e.getMessage());
e.printStackTrace();
ApiException apiException = (ApiException) e;
Log.e("LOG ", "status code " + apiException.getStatusCode());
}
});

Check Statistics for your links

Huawei App Linking provides lot many additional features to track the statistic for the created in-app links for your application which will be helpful in order to generate the analytical reports and improve the performance.

Step 1: Click on Statistics

Step 2: Check the performance graph based on the filters

Results

Tips and Tricks

1. URL prefix should be unique and can contain only lowercase letters and digits.

2. Link name and Default deep link can be same and follow as https://domainname.com/xxx

Where “domainname” is URLprefix which we set above and “xxx” is specific in-app content page to re-direct.

3. You can customize the deep link, which is different from the URL prefix of the link.

4. Ensure that the App Store ID and team ID have been configured. If they are not configured, add them as prompted.

Conclusion

This article focuses explains how in-app links can be created using Huawei App Linking capabilities and use them further for in –app content re-directions for large scale android applications. This article also explains how one can check the statistics for their app links.

Reference

https://developer.huawei.com/consum...ry-connect-Guides/agc-applinking-introduction