DEV Community

Zachary Powell
Zachary Powell

Posted on

2

Sharing In-app Images with Huawei's App Linking

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, 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

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

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

Open the project-level gradle file, and add the Maven repository address and AppGallery Connect plugin to the file.

buildscript {
repositories {
google()
jcenter()
maven { url 'http://developer.huawei.com/repo/' }
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
// Note: Configure your app dependencies
// in the app-level build.gradle file.
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'http://developer.huawei.com/repo/' }
}
}
view raw build.gradle hosted with ❤ by GitHub

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

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
android{…}
dependencies {
….
implementation 'com.huawei.agconnect:agconnect-applinking:1.6.0.300'
}
view raw build.gradle hosted with ❤ by GitHub

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.

private static final String DOMAIN_URI_PREFIX = "https://photoplaza.drcn.agconnect.link";
private static final String DEEP_LINK = "https://photoplaza-agc.dra.agchosting.link";
private static final String SHARE_DEEP_LINK = "photo://photoplaza.share.com";
/**
* Call AppLinking.Builder to create a link of App Linking in your app.
*
* @param UserName input UserName
* @param PhotoID input PhotoID
* @param ImageUrl input ImageUrl
* @param icallback input icallback
*/
public void createShareLinking(String UserName, String PhotoID, String ImageUrl, Icallback icallback) {
String newDeep_Link = SHARE_DEEP_LINK + "?PhotoID=" + PhotoID;
AppLinking.Builder builder = AppLinking.newBuilder()
.setUriPrefix(DOMAIN_URI_PREFIX)
.setDeepLink(Uri.parse(DEEP_LINK))
.setAndroidLinkInfo(AppLinking.AndroidLinkInfo.newBuilder()
.setAndroidDeepLink(newDeep_Link)
.build())
.setSocialCardInfo(AppLinking.SocialCardInfo.newBuilder()
.setTitle("It is a beautiful Photo")
.setImageUrl(ImageUrl)
.setDescription(UserName + " share a Photo to you")
.build())
.setCampaignInfo(AppLinking.CampaignInfo.newBuilder()
.setName("UserSharePhoto")
.setSource("ShareInApp")
.setMedium("WeChat")
.build());
builder.buildShortAppLinking().addOnSuccessListener(shortAppLinking -> {
shortLink = shortAppLinking.getShortUrl().toString();
try {
icallback.onSuccess(shortLink, "Success");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}).addOnFailureListener(e -> icallback.onFailure(e.getMessage()));
}
view raw AppLinking.java hosted with ❤ by GitHub

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

<activity android:name=".ImageDetailActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ImageListActivity" />
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Enter your custom domain name. -->
<data android:host="photoplaza.share.com" android:scheme="photo" />
</intent-filter>
</activity>

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.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setDarkStatusIcon();
setContentView(R.layout.image_detail);
AGConnectAppLinking.getInstance().getAppLinking(ImageDetailActivity.this)
.addOnSuccessListener(resolvedLinkData - > {
// The user has installed and used your app and the app is opened.
if (resolvedLinkData != null) {
Uri deepLink = resolvedLinkData.getDeepLink();
Log.i(TAG, "Open From AppLinking:" + deepLink);
// Obtain the image details.
getSharePicDetails(deepLink.toString());
}
}).addOnFailureListener(e - > {
Bundle data = getIntent().getExtras();
if (data != null) {
if (data.getBoolean("firstLink")) {
// The user has just installed your app and the app is opened for the first time.
getSharePicDetails(data.getString("deepLink"));
} else {
// Open the ImageDetail page.
initView();
}
}
});
}
/**
* Receive the image details from the link of App Linking, which is shared by the user.
*
* @param link input a deepLink of App Linking
*/
private void getSharePicDetails(String link) {
ToastUtils.showToast(this, getResources().getString(R.string.loading_photo));
sharePhotoID = parseLink(link).get("PhotoID");
Log.i("AppLinking", "sharePhotoID: " + sharePhotoID);
}
/**
* Obtain the data of the deep link of App Linking.
*
* @param url input the received deepLink
* @return myMap output the param
*/
public static Map < String, String > parseLink(String url) {
Map < String, String > myMap = new HashMap < String, String > ();
String[] urlParts = url.split("\\?");
String[] params = urlParts[1].split("&");
for (String param: params) {
String[] keyValue = param.split("=");
myMap.put(keyValue[0], keyValue[1]);
}
return myMap;
}

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.

AGConnectAppLinking.getInstance().getAppLinking(LoginActivity.this).addOnSuccessListener(resolvedLinkData -> {
// Your app is launched for the first time through the link of App Linking.
Log.i(TAG,"StartUp From AppLinking");
if (resolvedLinkData!= null) {
String deepLink = resolvedLinkData.getDeepLink().toString();
Log.i("AppLinking", "deepLink:" + deepLink);
Bundle bundle = new Bundle();
bundle.putBoolean("firstLink", true);
bundle.putString("deepLink", deepLink);
Intent intent;
intent = new Intent(LoginActivity.this, ImageDetailActivity.class);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
}).addOnFailureListener(e-> {
// Your app is launched normally.
Log.i(TAG,"Normal StartUp");
initView();
});

References

Service introduction of App Linking
Get Started tutorials
App Linking Videos: Troubleshooting

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more