tencent cloud

Feedback

Opening Mini Programs

Last updated: 2024-11-21 17:26:07
    The mini program management APIs provide capabilities for searching, opening, and closing mini programs.

    Opening mini programs

    When opening a mini program, the system will check whether there is a locally cached one. If not, it will automatically download the mini program from the remote server and then open it. If a cached version is available, the local mini program will be opened first while the system checks in the background for any new versions on the server.
    Notes:
    If a new version is available, it will be downloaded, and the next time you open the mini program, the updated version will be used.

    1. Opening a mini program by mini program ID (appId)

    To open the released version of a mini program :
    Notes:
    The appId field is the ID of the mini program, which can be obtained from the mini program developer or via the mini program search API.
    The miniStartOptions are the startup parameters for the mini program. For details, see API Description.
    TmfMiniSDK.startMiniApp(activity, appId, miniStartOptions);
    To open the Preview or development version of a mini program:
    TmfMiniSDK.startMiniApp(activiy, appId, MiniScene.LAUNCH_SCENE_MAIN_ENTRY, appVerType, options)
    Notes:
    The appVerType should match the version of the mini program. The value of the appVerType can be obtained from the MiniApp object instance returned by the API (getRecentList).
    The value of appVerType for the Preview of the mini program should be MiniApp.TYPE_PREVIEW.
    The value of appVerType for the development version of the mini program should be MiniApp.TYPE_DEVELOP.

    2. Opening a mini program through QR code

    The mini program SDK provides an API to open mini programs based on QR code scanning in the console. Before using the scanning capability provided by the mini program SDK, you need to integrate the scanning extension capability. For details about the integration, refer to the scanning capability documentation.
    After the scanning capability is integrated, you can start the QR code scanning and open the mini program with one of the options:
    Option 1:Process the scanning results by host app’s Activity
    Start QR code scanning:
    TmfMiniSDK.scan(activity);
    Get the result of the QR code scanning:
    Notes:
    The result of the QR code scanning is returned through the Activity's onActivityResult method. To identify the QR code, you need to rewrite the Activity's onActivityResult method and call TmfMiniSDK.getScanResult to get the QR code.
    @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data);
    // Get QR code scanning result JSONObject scanResult = TmfMiniSDK.getScanResult(requestCode, data); }
    Open the mini program according to the QR code scanning result:
    Notes:
    The getScanResult method returns data in a JSON structure. The mini program's startup parameters are stored in the 'result' field of this JSON structure. You need to extract the value of the `result` field and use it to open the mini program.
    @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data);
    //Get QR code scanning result JSONObject scanResult = TmfMiniSDK.getScanResult(requestCode, data);
    if (scanResult != null) { // Extract `result` field String result = scanResult.optString("result"); if (!TextUtils.isEmpty(result)) { MiniStartLinkOptions options = new MiniStartLinkOptions();
    //Use the value of the `result` field to open the mini program TmfMiniSDK.startMiniAppByLink(this, result, options); } }
    Option 2:Process the scanning results automatically by the agent Activity and start up the mini program directly
    Scan the QR code and start up the mini program:
    TmfMiniSDK.startMiniAppByScan(activity);

    3. Configuring Scheme to open the mini program

    Mini programs can be opened by third-party apps and scanning tools via URL redirection. Before you can use this capability, you need to configure the URL Scheme of the host app by adding the following string resource to the app:
    <string name="mini_sdk_intent_filter_scheme">your scheme</string>
    where `your scheme` should be replaced by the host's Scheme.
    The host of the mini program URL is applet and the full format of the URL is: ${scheme}://applet?appid=${appId}&amp;path=${encoded path}&amp;param=${encoded param}. Parameters in the URL are as follows:
    Parameter
    Type
    Required
    Description
    appId
    String
    Yes
    Mini program ID
    path
    String
    No
    Mini program entry path, for which the URL encoding is required
    param
    String
    No
    The query passed to the mini program, for which the URL encoding is required
    In the TCMPP console, a corresponding URL is created for the released version of each mini program and a QR code is generated based on the URL. When a URL is created, tcmpp+ host app’s appkey is used as the Scheme by default.
    
    
    
    To ensure that a correct URL is generated, please replace it with the host app’s URL Scheme. Click <b>Modify</b> to change the Scheme used to generate the URL:
    
    
    
    Once configured, you can open the mini program by scanning the generated QR code or via URL redirection.

    4. Opening a mini program by search

    Call the mini program search API:
    Notes:
    The parameters for SearchOptions are:
    keyword: Mini program keyword
    firstCate: Primary category of the mini program
    sencondaryCate: Secondary category of the mini program
    If both primary and secondary categories are provided, the search results will be the intersection of both categories.
    If only one category is provided, the search will be based on that single category.
    SearchOptions searchOptions = new SearchOptions(keyword, firstCate, sencondaryCate);
    TmfMiniSDK.searchMiniApp(searchOptions, new MiniCallback&lt;List<MiniApp>>() { @Override public void value(int code, String msg, List<MiniApp> data) { if (code == MiniCode.CODE_OK &amp;&amp; data != null) { // Search succeeded. The mini program list is not empty } else {
    // Search failed or the mini program list is empty } } });
    Open the mini program from the search results:
    Notes:
    The search results are returned via the value method of the MiniCallback API.
    The parameter int code indicates the error code.
    The parameter String msg indicates the returned search information.
    The parameter List<MiniApp> data indicates the list of found mini programs.
    SearchOptions searchOptions = new SearchOptions(keyword, firstCate, sencondaryCate);
    TmfMiniSDK.searchMiniApp(searchOptions, new MiniCallback&lt;List<MiniApp>>() { @Override public void value(int code, String msg, List<MiniApp> data) { if (code == MiniCode.CODE_OK &amp;&amp; data != null) { // Search succeeded. The mini program list is not empty
    MiniApp miniApp = data.get(0);
    // Open the first mini program in the search results TmfMiniSDK.startMiniApp(this,miniApp.appId,MiniScene.LAUNCH_SCENE_SEARCH,MiniApp.TYPE_ONLINE,new MiniStartOptions()); } else {
    // Search failed or the mini program list is empty } } });

    5. Mini program startup listener

    To help developers troubleshoot issues, the mini program SDK provides a listener for startup errors. You can configure it with the resultReceiver in MiniStartOptions.
    How to configure:
    private ResultReceiver mResultReceiver = new ResultReceiver(new Handler()) {
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
    if (resultCode != MiniCode.CODE_OK) {
    String errMsg = resultData.getString("errMsg");
    Toast.makeText(mActivity, errMsg + resultCode, Toast.LENGTH_SHORT).show();
    }
    }
    };
    The miniStartOptions.resultReceiver can be used to receive error codes when a mini program is started up. For error code details, see API Description.
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support