tencent cloud

Feedback

Opening mini programs

Last updated: 2024-06-27 11:09:21
    The mini program management API provides instructions for searching, opening, and closing mini programs.

    Opening mini programs

    When opening a mini program, the system first checks for a cached version locally. If none is found, it downloads the mini program from the remote server. If a cached version exists, it opens the local version while checking for updates in the background.
    Note:
    If a new version is available, download the new mini program and use it the next time you open it.

    1. Opening a mini program by ID (appID)

    To open a released mini program (approved and published), use the following code:
    Note:
    `appId` is the mini program's ID, obtainable from the developer or search interface.
    miniStartOptions is the startup parameters. For details, see API Description.
    TmfMiniSDK.startMiniApp(activity, appId, miniStartOptions);
    To open a preview or development version, use:
    TmfMiniSDK.startMiniApp(activiy, appId, MiniScene.LAUNCH_SCENE_MAIN_ENTRY, appVerType, options)
    Note:
    `appVerType` should match the mini program version, typically obtained from the MiniApp object instances from getRecentList API.
    The appVerType parameter value of a preview mini program should be MiniApp.TYPE_PREVIEW.
    The appVerType parameter value of a development mini program should be MiniApp.TYPE_DEVELOP.

    2. Opening a mini program by QR code scanning

    The SDK provides APIs to open mini programs using QR code content. Before using this feature, integrate the scanning capability as described in the scanning capability support documentation.
    After the scanning capability is integrated, you can start the QR code scanning and open the mini program:
    Start QR code scanning:
    TmfMiniSDK.scan(activity);
    Get QR code scaning results:
    Note:
    The scanning result of the QR code is returned through the Activity's onActivityResult method. To identify the QR code content, you need to rewrite the Activity's onActivityResult method and call TmfMiniSDK.getSfcanResult to obtain the content of 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:
    Note:
    The getScanResult method returns data in a JSON structure. The mini program's startup parameters are stored in the result field of this JSON. 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 a mini program
                   TmfMiniSDK.startMiniAppByLink(this, result, options);
               }
           }
        }

    3. Opening a mini program by searching

    Call the mini program searching API:
    Note:
    The parameters of SearchOptions are:
    keyword: Mini program keyword
    firstCate: Primary category of mini programs
    sencondaryCate: Secondary category of mini programs
    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<List<MiniApp>>() { @Override public void value(int code, String msg, List<MiniApp> data) {
           if (code == MiniCode.CODE_OK && data != null) {
              //Search succeeded. Mini program list is not empty
           } else {
             //Search failed or mini program list is empty
           }
        }
    });
    Open the mini program from search results:
    Note:
    The search results are returned via the value method of the MiniCallback API.
    Parameter `int code` indicates error code.
    Parameter `string msg` indicates the returned search message.
    Parameter `List< MiniApp> data` indicates the list of found mini programs.
    SearchOptions searchOptions = new SearchOptions(keyword, firstCate, sencondaryCate);
     
    TmfMiniSDK.searchMiniApp(searchOptions, new MiniCallback<List<MiniApp>>() {
       @Override
       public void value(int code, String msg, List<MiniApp> data) {
           if (code == MiniCode.CODE_OK && data != null) {
              //Search succeeded. 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 mini program list is empty
           }
        }
    });

    4. Mini program startup listener

    To help developers troubleshoot issues, the mini program SDK provides a listener for startup errors. You can set this up using the resultReceiver in MiniStartOptions.
    Here's how to do it:
    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 launching a mini program. 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