当前位置:网站首页>[applinking practical case] share in app pictures through applinking

[applinking practical case] share in app pictures through applinking

2022-07-02 11:00:00 Huawei Developer Forum

Huawei AppGallery Connect Provided App Linking Service support , It can support cross platform sharing , Support in Android,iOS,Web And other platforms . about Android and iOS Mobile platform , You can directly pull up the specified page of the application , If the specified application is not installed on the device ,App Linking Links can also pull up stores to download apps , After downloading the app , You can also open the interface of the specified application for the first time .

Here's a , Through Huawei App Linking SDK, Scene sharing link , It is used to share the actual use scenarios of photos in the app .

Demand analysis

First, study Huawei App Linking Official documents : App Linking- Getting started

From the steps of official documents , Integrating Huawei App Linking service , It mainly involves four steps

  1. stay AGC Management desk , Create the corresponding AGC project .
  2. Create a specific link prefix in the project
  3. stay Android Integration in the project App Linking SDK, And create links flexibly through code
  4. To configure Android The application receives and processes App Linking link .

From the perspective of code requirements and configuration , Only the corresponding PhotoID, Downstream picture loading link , You can go through PhotoID Realize the recognition and loading of photos .

therefore , Just put PhotoID Add to App Linking The share of DeepLink in , Then when receiving , Through to DeepLink Parsing , To get the corresponding PhotoID.

Step1: establish AGC project

  1. stay AGC Console ,AppGallery Connect (huawei.com), Select my project in the interface .
  2. Select... On the interface + Number Add an item . Enter the corresponding project name .
  3. Add the corresponding application for the project , Here you need to configure the corresponding package name , Package name needs and Android Keep consistent in the project code .

1.png

Step2: Create a link prefix

  1. Under the project created in the previous step , Left menu bar , choice growth – App Linking, Enter into App Linking Tab .
  2. Click to use now , Open this service .
  3. Select “ Link prefix ” Tab , First add a link prefix , This prefix needs to be unique in the whole network , The system will automatically check your uniqueness .

For example, the link prefix I configured is as follows :

2.png

Step3: Integrate SDK、 Use API Create links

  1. go back to AGC Project Settings page , download agconnect-service.json file , Put it in Android Project App Under the path

3.png

    2. open Android Project level gradle file , Add the following AGC Maven Warehouse address and AGCP plug-in unit .

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: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'http://developer.huawei.com/repo/' }
    }
}

    3. stay Android Application level gradle In file , application AGCP plug-in unit , And add App Linking SDK.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

android{…}


dependencies {
  ….
implementation 'com.huawei.agconnect:agconnect-applinking:1.6.0.300'
}

    4. Use API Interface , according to PhotoID Flexible creation App Linking link :

Note here , I introduced UserName and ImageURL Parameters , As a preview page when sharing , In addition, we will PhotoID This parameter is encapsulated in DeepLink in .

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 App Linking in 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()));
    }

Step4: receive App Linking Connect .

Let's talk about planning first , I plan on ImageDetailActivity This page , Conduct App Linking Link reception , in other words , Link directly after pulling up the application , It's this that opens Detail page .

  1. To configure AndroidManifest file . find ImageDetailActivity label , Add the need to receive DeepLink Of Intent-filter
<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" />
                <!--  Add the customized domain name in the project here  -->
                <data android:host="photoplaza.share.com" android:scheme="photo" />
            </intent-filter>
        </activity>

2. open ImageDetail file , stay onCreate() Configure receive and parse App Linking And get DeepLink Related code

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setDarkStatusIcon();
        setContentView(R.layout.image_detail);
        AGConnectAppLinking.getInstance().getAppLinking(ImageDetailActivity.this)
                .addOnSuccessListener(resolvedLinkData -> {
            //  adopt AppLinking Start the application , Here is the restart of the installed scenario 
            if (resolvedLinkData!= null) {
                Uri deepLink = resolvedLinkData.getDeepLink();
                Log.i(TAG, "Open From AppLinking:"  + deepLink);
                // Get picture details.
                getSharePicDetails(deepLink.toString());
            }
        }).addOnFailureListener(e->{
            Bundle data = getIntent().getExtras();
            if (data != null) {
                if (data.getBoolean("firstLink")) {
					//  adopt AppLinking The entry to start the application for the first time 
                    getSharePicDetails(data.getString("deepLink"));
                }
                else{
                    //  Normal open ImageDetail page 
                    initView();
                }
            }
        });
}

    /**
     * Get a picture detail from a App Linking which share from other.
     *
     * @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);
    }

    /**
     * Get param in received deepLink from AppLinking.
     *
     * @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;
    }

Step5: Additional steps

because App Linking It will also be used in scenarios where the application is not installed . In this step, specific users are introduced to the scenario of new installation ,App Linking What kind of adaptation is needed .

  1. A description of the situation :

First, for applications that are not installed ,App Linking The link will be based on the specified package name ( The default is the one when creating the link App The package name ), According to the package name , Open the app download page of Huawei store of your mobile phone , Of course, you can also configure the local store to open .

After downloading and installing in the store , Click on the open ,App Linking It is also effective and can pass parameters . But because it is the first time to open , At this point, step 4 configures Intent-filter It doesn't work . Only the homepage of the application can be opened . Because the focus of this step , It's in the startup page of the application , adapter App Linking The scene opened for the first time .

    2. Code adaptation , Open the app launch page , Same configuration getAppLinking Code for

For example, my startup page is LoginActivity,

AGConnectAppLinking.getInstance().getAppLinking(LoginActivity.this).addOnSuccessListener(resolvedLinkData -> {
            //  adopt AppLinking For the first time to start 
            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-> {
            //  Normal startup mode 
            Log.i(TAG,"Normal StartUp");
            initView();
        });

Summarize and sum up

above , Namely Android Within the project , adopt App Linking Of SDK, The ability to quickly build in app image sharing , There is no need to apply for an exclusive domain name , It also does not involve the operation and maintenance of the background 、H5 Page development 、JavaScript Code adaptation and so on , Easy five steps to complete . It can be said to be very convenient and fast .

The following is a screenshot of the simple steps of the page involved :

4.png      5.png      6.png

For more details , Please see the :
Huawei official website :
https://developer.huawei.com/consumer/cn/forum/topic/0202652719220650728?fid=0101271690375130218?ha_source=zzh

Reference documents

  1. Huawei AGC App Linking Service business introduction :App Linking- Business Introduction
  2. Huawei ACG Android Getting started with the platform :App Linking- Getting started
  3. Huawei AGC App Linking service , Troubleshooting common problems :App Linking- Video class : Troubleshooting common problems
原网站

版权声明
本文为[Huawei Developer Forum]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/183/202207020715388060.html