We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
Sunset Announcement: The HMS Toolkit service will no longer receive version updates. Services in overseas regions will be decommissioned on December 30, 2026, and will no longer be available after sunset.
Tools GuidesHMS ToolkitUsage GuideConvertor Manual Conversion GuideAppGallery Connect Manual Conversion GuideCloud Functions

Cloud Functions

Preparations

  1. If you want your app to support Cloud Functions, complete the integration preparation by referring to Getting Started with Android.
  2. For details about Cloud Functions, please refer to Cloud Functions. For details about related APIs, please refer to Cloud Functions API Reference.

getCode()

This method is used to obtain the result codes of Firebase Functions exceptions. The return value is a code of the enum type, while that of AGCFunctionException.getCode() in AppGallery Connect is of the int type. AppGallery Connect does not have so many types of possible errors as defined in Firebase, and common errors are contained in various exception types. Therefore, when using AppGallery Connect, you can capture error messages using various types of common exceptions.

When the getCode() method of Firebase is used:

try {
    // ...
} catch (FirebaseFunctionsException e){
    FirebaseFunctionsException.Code code = e.getCode();
    // ...
}

The following is an example of code modification in the Add HMS API policy (after the conversion, the generated XMS Adapter module contains the GlobalEnvSetting and other classes):

try {
    // ...
} catch (ExtensionFunctionsException e){
    if (GlobalEnvSetting.isHms()) {
        int code = ((com.huawei.agconnect.function.AGCFunctionException) e.getHInstance()).getCode();
    } else {
        FirebaseFunctionsException.Code code = ((com.google.firebase.functions.FirebaseFunctionsException) e.getGInstance()).getCode();
    }  
}

The following is an example of code modification in the To HMS API policy:

try {
    // ...
} catch (AGCFunctionException e){
    int code = e.getCode();
    // ...
}

Methods Related to getInstance

getInstance(FirebaseApp app) creates a Cloud Functions client using a given app.

getInstance(FirebaseApp app, String region) creates a Cloud Functions client using a given app and region.

getInstance(String region) creates a Cloud Functions client using the default app and a specified region.

In Firebase, getInstance in the preceding methods is implemented with parameters, but without parameters in AppGallery Connect. The following provides the simplified sample code.

When the getInstance() method of Firebase is used:

HttpsCallableReference httpsCallableReference = FirebaseFunctions.getInstance().getHttpsCallable("test0");
HttpsCallableReference httpsCallableReference = FirebaseFunctions.getInstance("test1").getHttpsCallable("test1");
HttpsCallableReference httpsCallableReference = FirebaseFunctions.getInstance(FirebaseApp.getInstance()).getHttpsCallable("test2");
HttpsCallableReference httpsCallableReference = FirebaseFunctions.getInstance(FirebaseApp.getInstance(), "test3").getHttpsCallable("test3");

The following is an example of code modification in the To HMS API policy:

FunctionCallable httpsCallableReference = AGConnectFunction.getInstance().wrap("test0");
FunctionCallable httpsCallableReference = AGConnectFunction.getInstance().wrap("test1");
FunctionCallable httpsCallableReference = AGConnectFunction.getInstance().wrap("test2");
FunctionCallable httpsCallableReference = AGConnectFunction.getInstance().wrap("test3");

void useFunctionsEmulator(String)

This method is used to call a local function simulator to help developers test cloud functions. In AppGallery Connect, you do not need to add code to your program. Instead, you only need to perform some web operations for function testing. For details, please refer to Testing a Function.

Search in Operation Guide
Enter a keyword.