目录

Cloud Functions

准备工作

  1. 您若需要应用支持Cloud Functions,必须完成接入准备,请参见AppGallery Connect开发服务使用入门。
  2. 华为云函数服务详情请参见云函数服务简介,相关API详情请参见云函数API参考。

getCode()

此API的作用是获取Firebase云函数异常的错误码,返回值是一个枚举类型Code,而在AGC中,AGCFunctionException.getCode()返回的是一个int值。在AGC中,并不会出现Firebase中定义的那么多种类型的可能错误,常见的错误包含在各种Exception类型中,因此使用AGC时,直接使用各种常见Exception即可捕捉到相应错误信息。

使用Firebase的getCode()方法的代码场景:

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

在Add HMS API模式下的代码修改示例(在插件转换后,生成的xmsadapter模块中会包含下面用到的GlobalEnvSetting等类):

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();
    }  
}

在To HMS API模式下的代码修改示例:

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

getInstance相关方法

getInstance(FirebaseApp app)使用给定的应用创建云函数客户端。

getInstance(FirebaseApp app, String region)使用给定的应用和区域创建云函数客户端。

getInstance(String region)使用默认的应用和指定区域创建云函数客户端。

如上三个带参数的getInstance在AGC中对应的getInstance均为无参实现。下面给出简易的代码示例。

使用Firebase的getInstance()方法的代码场景:

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");

在To HMS API模式下的代码修改示例:

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)

该API的作用是调用本地函数模拟器,能够帮助您测试云端函数。而在AGC中,无需在您的程序中添加代码,只需要通过一些Web上的操作就能实现函数功能的测试,请参见测试函数。