All Products
Search
Document Center

Mobile Platform as a Service:Manage offline packages

Last Updated:Mar 03, 2023

Offline package management operations include: Preload an HTML5 app, Use the global resource package, Update an HTML5 app, Download an HTML5 app, Install an HTML5 app, Obtain app information, Verify the security signature, and Delete local app information.

Prerequisite

Preload an HTML5 app

Generally, the offline package may have not been downloaded when an HTML5 app is opened for the first time. In this case, you need to open the app by using the fallback URL.

Preloading an HTML5 app involves preinstalling an available HTML5 app in the installation package released by the client. When opening a preinstalled app for the first time, the user can directly use the offline package resources, which improves user experience.

Note

We recommend that you preinstall only core HTML5 apps and do not preinstall apps with a low usage rate.

To preload an HTML5 App, you need to complete the following steps:

  1. Download the HTML5 app configuration file .json and required offline packages from the HTML5 app release backend.

  2. Add the .json file and offline packages to the asset directory of the project.

  3. When the app is started, call the preload code to install the app. The code sample is as follows:

    MPNebula.loadOfflineNebula("h5_json.json", new MPNebulaOfflineInfo("90000000_1.0.0.6.amr", "90000000", "1.0.0.6"));
    Note

    • This method is a block-type call method. Do not call the offline package method embedded on the main thread.

    • This method can be called only once. If it is called multiple times, only the first call is valid. Therefore, you need to input the information about all required offline packages at a time.

    • If multiple amr packages are embedded, ensure that the file already exists. If it does not exist, other embedded offline packages will fail.

Use the global resource package

The Nebula global resource package addresses redundancy caused by multiple HTML5 apps using the same resource. For example, the React app uses the ReactJS framework code. You can include public resources into the global resource package to downsize the HTML5 app.

Generally, a global resource package needs to be preset for the project, and subsequent updates can still be delivered through the HTML5 app backend.

The following code sample specifies the resource package with the app ID (appId) 66666692 as the global resource package and presets the assets/nebulaPreset/66666692 offline package.

  • getCommonResourceAppList: notifies the HTML5 container that an offline package with the specified ID will be used as the global resource package. If this ID is not configured, this offline package does not take effect even if it is embedded.

  • getH5PresetPkg: specifies the path and version of the embedded global resource package. The HTML5 container loads the resource package from the specified asset resource directory. However, if a later version is detected on the server, this embedded package of an earlier version will not be loaded. In addition, you can use the aforementioned loadOffLineNebula method to preset a global resource package. In this case, you do not need to specify the path and version of the embedded offline package in this method.

    Note

    This method applies only to an HTML5 global resource package.

  • getTinyCommonApp: returns the framework resource package ID of a mini app. If your global resource package is used only by the HTML5 container, do not write the ID of this public resource package in this method.

Reference code sample:

public class H5AppCenterPresetProviderImpl implements H5AppCenterPresetProvider {
    private static final String TAG = "H5AppCenterPresetProviderImpl";

    // The public resource package of the business, try to avoid the beginning of 666666
    private static final String COMMON_BIZ_APP = "xxxxxxxx";

    // Special resource package for mini programs, do not move for business
    private static final String TINY_COMMON_APP = "66666692";

    // The asset directory of the preset package
    private final static String NEBULA_APPS_PRE_INSTALL = "nebulaPreset" + File.separator;

    // Preset package collection
    private static final Map<String, H5PresetInfo> NEBULA_LOCAL_PACKAGE_APP_IDS = new HashMap();

    static {

        H5PresetInfo h5PresetInfo2 = new H5PresetInfo();
        // File name of the built-in directory
        h5PresetInfo2.appId = TINY_COMMON_APP;
        h5PresetInfo2.version = "1.0.0.0";
        h5PresetInfo2.downloadUrl = "";

        NEBULA_LOCAL_PACKAGE_APP_IDS.put(TINY_COMMON_APP, h5PresetInfo2);

    }

    @Override
    public Set<String> getCommonResourceAppList() {
        Set<String> appIdList = new HashSet<String>();
        appIdList.add(getTinyCommonApp());
        appIdList.add(COMMON_BIZ_APP);
        return appIdList;
    }

    @Override
    public H5PresetPkg getH5PresetPkg() {
        H5PresetPkg h5PresetPkg = new H5PresetPkg();
        h5PresetPkg.setPreSetInfo(NEBULA_LOCAL_PACKAGE_APP_IDS);
        h5PresetPkg.setPresetPath(NEBULA_APPS_PRE_INSTALL);
        return h5PresetPkg;
    }

    @Override
    public Set<String> getEnableDegradeApp() {
        return null;
    }

    @Override
    public String getTinyCommonApp() {
        return TINY_COMMON_APP;
    }

    @Override
    public InputStream getPresetAppInfo() {
        return null;
    }

    @Override
    public InputStream getPresetAppInfoObject() {
        return null;
    }
}

Call the following code when the App starts:

H5Utils.getH5ProviderManager().setProvider(H5AppCenterPresetProvider.class.getName(), new H5AppCenterPresetProviderImpl());

Update an HTML5 app

By default, Nebula checks for a later version each time an HTML5 app is opened. Considering the pressure on the server, the check duration is restricted and is 30 minutes by default. To immediately check for the latest available version, call the following code to request for an update. Generally, the code can be called after the app starts or after the user logs on.

MPNebula.updateAllApp(new MpaasNebulaUpdateCallback(){
    @Override
    public void onResult(final boolean success, final boolean isLimit) {
        super.onResult(success, isLimit);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AUToast.makeToast(NebulaAppActivity.this,
                        success ? R.string.update_success : R.string.update_failure, 2000).show();
            }
        });
    }
});

Download an HTML5 app

The MPNebula API allows you to manually download an HTML5 app.

/**
     *Download an offline package.
     *
     * @param appId: offline package ID
     * @param mpaasNebulaDownloadCallback: download callback
     */
    public static void downloadApp(final String appId, final MpaasNebulaDownloadCallback mpaasNebulaDownloadCallback)

Install an HTML5 app

The MPNebula API allows you to manually install an HTML5 app.

/**
     *Install an offline package.
     *
     * @param appId: offline package ID
     * @param mpaasNebulaInstallCallback: installation callback
     */
    public static void installApp(final String appId, final MpaasNebulaInstallCallback mpaasNebulaInstallCallback)

Obtain app information

Call the H5AppProvider method to obtain related information about HTML5 apps.

H5AppProvider provider = H5Utils.getProvider(H5AppProvider.class.getName());
AppInfo appInfo = provider.getAppInfo("1000000"); // Obtain app configuration.
boolean isInstalled = provider.isInstalled("100000", "1.0.0.0"); // Whether an app version is installed.
boolean isAvailable = provider.isAvailable("100000", "1.0.0.0");// Whether the offline package of an app version is downloaded successfully.

Verify the security signature

Nebula has an offline package signature verification mechanism to prevent malicious programs from tampering with offline packages downloaded to the device. Call MPNebula interface to use signature verification. In 10.1.60 and above, additional container configuration is required. See HTML5 container configuration for details.

Note

  • Call this API before opening the offline package for the first time. Otherwise, public key initialization fails. About public and private keys, see Configure offline packages.

  • Signature verification is forcibly performed on a mobile phone judged as root, regardless of whether signature verification is enabled on the client.

```java
/**
     * @param publicKey: public key for signature verification
     */
    public static void enableAppVerification(final String publicKey)
```

Delete local app information

Nebula provides the interface for deleting local app information. After local app information is deleted, the client side will send request to the service side to download and update local app when the app is opened again.

public class MPNebula {
    // appId is the ID of the offline package or the mini program
    public static boolean deleteAppInfo(String appId);
}
Note

The lowest baseline versions supported by this API are 10.1.68.8 and 10.1.60.14 respectively.