Sharing In-app Images with Huawei's App Linking
Tags: android, huawei, appgallery
Published: 2022-02-21 14:18:12

App Linking allows you to create links that work across multiple platforms including Android, iOS, and web. When an Android or iOS device user taps a link created with App Linking, they will be redirected to the specific in-app content. If a user has not yet installed the app, they will be redirected to their local app store to download the app. After downloading and opening the app, the user will be taken to the in-app content.

The following shows you how to share in-app images with App Linking.

## Service Setup

To integrate the [service](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-applinking-getstarted-android-0000001054594767.), perform the following steps:

1.	Create a project in AppGallery Connect.
2.	Request a URL prefix.
3.	Integrate the service SDK into your app and create a link using an API.
4.	Configure your Android app to receive and process the link of App linking.

To identify and load an image to be shared, you need to add the PhotoID parameter to the deep link of App Linking, and parse the deep link to obtain the parameter value when receiving the link.

### Creating a Project in AppGallery Connect
1.	Sign in to AppGallery Connect and click My projects.
    
2.	Click + to add a project on the displayed page. Enter a project name and click OK.

3.	Add an app to the project, and configure the same package name as that set in your Android project.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojcvquhospacd09dpapx.png)

### Requesting a URL Prefix

1.	Go to **Grow** > **App Linking**.

2.	On the **App Linking** page, click **Use now** to enable the service.

3.	Under the **URL prefixes** tab page, click **New URL prefix** to add a unique URL prefix. AppGallery Connect automatically checks whether the URL prefix is unique.
In this example, the URL prefix is configured as follows.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hll7hgndcd2zaceafij.png)

### Integrating the Service SDK and Creating a Link Using an API
 
Go to Project setting > General information, download the agconnect-service.json file and add it to the App directory of your project.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35wigutorilokh2abeeg.png)

Open the project-level **gradle** file, and add the Maven repository address and AppGallery Connect plugin to the file.
 
{% gist https://gist.github.com/devwithzachary/15fb79c03b35c14a01d883d7348fa3c4 %}

Apply the AppGallery Connect plugin in the app-level gradle file, and add the service SDK to the file.

{% gist https://gist.github.com/devwithzachary/235235236295a77b86bf45f286fefd0c %}

Call an API to create a link of App Linking based on the photo ID.
Define the **UserName** and **ImageURL** input parameters for the preview page of the deep link, and encapsulate the **PhotoID** parameter into the deep link.

{% gist https://gist.github.com/devwithzachary/4ff9f5e9ec8c11ec4b598c9cdff31b69 %}

### Receiving a Link of App Linking
First of all, create an activity on the ImageDetail page for receiving the deep link of App Linking, to which a user will be redirected after the link is tapped.

Add an intent filter for receiving the deep link to **ImageDetailActivity** in the **AndroidManifest** file

{% gist https://gist.github.com/devwithzachary/67788e796b36c1ea812127182e5b1dbc %}

Under onCreate() of the ImageDetail file, add the following code for receiving and parsing a link of App Linking, and for obtaining the data of the deep link.

{% gist https://gist.github.com/devwithzachary/cef29ec51eff81f4a373b97aac1e3cff %}

## Additional Configurations

If a user has not installed your app, perform the following operations:

If a user has not installed your app, they will be redirected to the app details page on AppGallery or another local app store, to download your app, based on the app package name of your app.

When the user downloads and launches your app, the link of App Linking can still take effect and parameter values can be passed. However, the user will be redirected to the home page upon the first launch your app, as the intent filter configured in the last step does not take effect. Therefore, you need to configure the home screen for receiving the link of App Linking.

So for example set an activity for getAppLinking, for example, LoginActivity.

{% gist https://gist.github.com/devwithzachary/3850b4e95bbf6fcb4e84f8d4ec3bb978 %}

References

[Service introduction of App Linking](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-applinking-introduction-0000001054143215)
[Get Started tutorials](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-applinking-getstarted-android-0000001054594767.)
[App Linking Videos: Troubleshooting](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-applinking-faqvideo-0000001189403609)