The mini program management APIs provide capabilities for searching, opening, and closing mini programs.
Open a mini program
When opening the 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.
Note:
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. Open a mini program by mini program appid
To open the released version of a mini program. See the example below:
Note:
The appid 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 preview or open the development version of a mini program:
TmfMiniSDK.startMiniApp(activiy, appId, MiniScene.LAUNCH_SCENE_MAIN_ENTRY, appVerType, options)
Note:
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 mini program preview should be MiniApp.TYPE_PREVIEW.
The value of appVerType for the development version of the mini program should be MiniApp.TYPE_DEVELOP.
2. Open a mini program through QR code
The mini program SDK provides an API to open mini programs based on QR code scanning. 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 QR code scanning extension SDK. After the scanning capability is integrated, you can start the QR code scanning and open the mini program with one of the options:
Method 1:Process the scanning results by superapp’s Activity
Start QR code scanning:
TmfMiniSDK.scan(activity);
Get the result of the QR code scanning:
Note:
The result of the QR code scan is returned through the onActivityResult method of the Activity. To identify the content of the QR code, you need to override the 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);
JSONObject scanResult = TmfMiniSDK.getScanResult(requestCode, data);
}
Open the mini program according to the QR code scanning result:
Note:
The getScanResult method returns a JSON structure, and the launch parameters for the mini program are stored in the 'result' field of this JSON data. You need to extract the value corresponding to the 'result' field, which will ultimately be used to open the mini program.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
JSONObject scanResult = TmfMiniSDK.getScanResult(requestCode, data);
if (scanResult != null) {
String result = scanResult.optString("result");
if (!TextUtils.isEmpty(result)) {
MiniStartLinkOptions options = new MiniStartLinkOptions();
TmfMiniSDK.startMiniAppByLink(this, result, options);
}
}
}
Method 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. Configure 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 superapp by adding the following string resource to the superapp:
<string name="mini_sdk_intent_filter_scheme">your scheme</string>
where your scheme should be replaced by the superapp's Scheme.
The host of the mini program URL is applet and the full format of the URL is: ${scheme}://applet?appid=${appId}&path=${encoded path}¶m=${encoded param}. Parameters in the URL are as follows:
|
appId | String | True | Mini program appid. |
path | String | False | Mini program entry path, for which the URL encoding is required |
param | String | False | The query passed to the mini program, for which the URL encoding is required |
In the 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 + superapp’s appkey is used as the Scheme by default.
To ensure that a correct URL is generated, please replace it with the superapp’s URL Scheme. Click Modify 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. Open a mini program by search
Call the mini program search API:
Note:
keyword: Mini program keyword
firstCate: Primary category of the mini program
secondaryCate: 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<List<MiniApp>>() {
@Override
public void value(int code, String msg, List<MiniApp> data) {
if (code == MiniCode.CODE_OK && data != null) {
} else {
}
}
});
Open the mini program from the search results:
Note:
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<List<MiniApp>>() {
@Override
public void value(int code, String msg, List<MiniApp> data) {
if (code == MiniCode.CODE_OK && data != null) {
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 {
}
}
});
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();
}
}
};
miniStartOptions.resultReceiver can be used to receive error codes when a mini program is started up. For error code details, see API Description.