转到控制台

从您的应用中调用函数

借助 Cloud Functions for Firebase 客户端 SDK,您可以直接从 Firebase 应用调用函数。如需以这种方式从您的应用调用函数,请在 Cloud Functions 中编写和部署 HTTPS Callable 函数,然后在您的应用中添加调用该函数的客户端逻辑。

Callable 函数与其他 HTTP 函数类似,另外还具有以下功能:

  • 使用 Callable 函数时,Firebase 身份验证和 FCM 令牌(如果可用)会自动包含在请求中。
  • functions.https.onCall 触发器会自动对请求正文进行反序列化,并核实身份验证令牌。

Firebase SDK for Cloud Functions(v0.9.1 及更高版本)可与下列 Firebase 客户端 SDK 最低版本交互,以支持 HTTPS Callable 函数:

  • Firebase SDK for iOS 6.8.1
  • Firebase SDK for Android 19.0.1
  • Firebase JavaScript SDK 6.6.2

如果您要在不受支持的平台上构建的应用中添加类似功能,请参阅 https.onCall 协议规范。本指南的其余部分提供了有关如何编写、部署和调用面向 iOS、Android、Web、C++ 和 Unity 平台的 HTTPS Callable 函数的说明。

编写和部署 Callable 函数

使用 functions.https.onCall 方法创建 HTTPS Callable 函数。该方法需要两个参数:data 和可选的 context

// Saves a message to the Firebase Realtime Database but sanitizes the text by removing swearwords.
exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});

例如,如果某个 Callable 函数用于将文本消息保存到实时数据库,则可以在 data 中包含消息文本,而用 context 参数表示用户身份验证信息:

// Message text passed from the client.
const text = data.text;// Authentication / user information is automatically added to the request.
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;

Callable 函数的位置与调用客户端位置之间的距离可能会产生网络延迟。为了优化性能,请考虑在适用时指定函数位置,并确保将 Callable 函数的位置与您在客户端初始化 SDK 时设置的位置保持一致。

返回结果

如需向客户端发回数据,请返回可以进行 JSON 编码的数据。例如,需要返回加法运算的结果:

// returning result.
return {
  firstNumber: firstNumber,
  secondNumber: secondNumber,
  operator: '+',
  operationResult: firstNumber + secondNumber,
};

如需在异步操作之后返回数据,请返回一个 Promise。Promise 返回的数据将发送给客户端。例如,您可以返回经过处理的 Callable 函数写入实时数据库的文本:

// Saving the new message to the Realtime Database.
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
  text: sanitizedMessage,
  author: { uid, name, picture, email },
}).then(() => {
  console.log('New Message written');
  // Returning the sanitized message to the client.
  return { text: sanitizedMessage };
})

处理错误

为了确保客户端获得有用的错误详情,可以通过抛出 functions.https.HttpsError 实例(或返回带有此实例的被拒 Promise)从 Callable 返回错误。此类错误有一个 code 特性,该特性可以是 functions.https.HttpsError 中列出的某个值。这些错误还有一个字符串 message,默认为空字符串。它们可能还有一个含任意值的可选 details 字段。如果从您的函数抛出一个非 HttpsError 错误,则您的客户端会收到带有消息 INTERNAL 和代码 internal 的错误。

例如,函数可以抛出数据验证和身份验证错误,并将错误消息返回给调用客户端:

// Checking attribute.
if (!(typeof text === 'string') || text.length === 0) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
      'one arguments "text" containing the message text to add.');
}
// Checking that the user is authenticated.
if (!context.auth) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
      'while authenticated.');
}

部署 Callable 函数

当您在 index.js 内保存已完成的 Callable 函数后,该函数会在您运行 firebase deploy 时与其他所有函数一起得到部署。如果只需要部署 Callable 函数,请按如下所示的方式使用 --only 参数执行部分部署

$ firebase deploy --only functions:addMessage

设置您的客户端开发环境

请确保您满足所有前提条件,并将必要的依赖项和客户端库添加到您的应用。

iOS

  1. 按照相关说明将 Firebase 添加到您的 iOS 应用
  2. 将 Cloud Functions Pod 添加到您的 Podfile
    pod 'Firebase/Functions'
  3. 保存文件,然后运行以下命令:
    pod install

Web

  1. 按照相关说明将 Firebase 添加到您的 Web 应用
  2. 将 Firebase 核心和 Cloud Functions 客户端库添加到您的应用:
    <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/6.6.2/firebase-functions.js"></script>
    

Cloud Functions SDK 也作为 npm 软件包提供。

  1. 从终端运行以下命令:
    npm install firebase@6.6.2 --save
    
  2. 需要手动通过 require() 函数引入 Firebase 核心和 Cloud Functions:
    const firebase = require("firebase");
    // Required for side-effects
    require("firebase/functions");
    

Java

  1. 按照相关说明将 Firebase 添加到您的 Android 应用
  2. 请务必在您的项目级 build.gradle 文件中的 buildscriptallprojects 部分添加 Google 的 Maven 代码库。
  3. 将 Cloud Functions Android 库的依赖项添加到您的模块(应用级)Gradle 文件(通常为 app/build.gradle):
    implementation 'com.google.firebase:firebase-functions:19.0.1'
    

Kotlin

  1. 按照相关说明将 Firebase 添加到您的 Android 应用
  2. 请务必在您的项目级 build.gradle 文件中的 buildscriptallprojects 部分添加 Google 的 Maven 代码库。
  3. 将 Cloud Functions Android 库的依赖项添加到您的模块(应用级)Gradle 文件(通常为 app/build.gradle):
    implementation 'com.google.firebase:firebase-functions:19.0.1'
    

C++

对于 Android 平台的 C++ 项目

  1. 按照相关说明将 Firebase 添加到您的 C++ 项目
  2. 请务必在您的项目级 build.gradle 文件中的 buildscriptallprojects 部分添加 Google 的 Maven 代码库。
  3. firebase_functions 库添加到 CMakeLists.txt 文件。

对于 iOS 平台的 C++ 项目

  1. 按照相关说明将 Firebase 添加到您的 C++ 项目
  2. 将 Cloud Functions Pod 添加到您的 Podfile
    pod 'Firebase/Functions'
  3. 保存文件,然后运行以下命令:
    pod install
  4. Firebase C++ SDK 中的 Firebase 核心和 Cloud Functions 框架添加到您的 Xcode 项目。
    • firebase.framework
    • firebase_functions.framework

Unity

  1. 按照相关说明将 Firebase 添加到您的 Unity 项目
  2. Firebase Unity SDK 中的 FirebaseFunctions.unitypackage 添加到您的 Unity 项目中。

初始化客户端 SDK

初始化 Cloud Functions 的实例:

Swift

lazy var functions = Functions.functions()

Objective-C

@property(strong, nonatomic) FIRFunctions *functions;// ...
self.functions = [FIRFunctions functions];

Web

firebase.initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FUNCTIONS PROJECT ID ###'
  databaseURL: 'https://### YOUR DATABASE NAME ###.firebaseio.com',
});

// Initialize Cloud Functions through Firebase
var functions = firebase.functions();

Java

private FirebaseFunctions mFunctions;// ...
mFunctions = FirebaseFunctions.getInstance();

Kotlin

private lateinit var functions: FirebaseFunctions// ...
functions = FirebaseFunctions.getInstance()

C++

firebase::functions::Functions* functions;
// ...
functions = firebase::functions::Functions::GetInstance(app);

Unity

functions = Firebase.Functions.DefaultInstance;

调用函数

Swift

functions.httpsCallable("addMessage").call(["text": inputField.text]) { (result, error) in
  if let error = error as NSError? {
    if error.domain == FunctionsErrorDomain {
      let code = FunctionsErrorCode(rawValue: error.code)
      let message = error.localizedDescription
      let details = error.userInfo[FunctionsErrorDetailsKey]
    }
    // ...
  }
  if let text = (result?.data as? [String: Any])?["text"] as? String {
    self.resultField.text = text
  }
}

Objective-C

[[_functions HTTPSCallableWithName:@"addMessage"] callWithObject:@{@"text": _inputField.text}
                                                      completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) {
  if (error) {
    if (error.domain == FIRFunctionsErrorDomain) {
      FIRFunctionsErrorCode code = error.code;
      NSString *message = error.localizedDescription;
      NSObject *details = error.userInfo[FIRFunctionsErrorDetailsKey];
    }
    // ...
  }
  self->_resultField.text = result.data[@"text"];
}];

Web

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({text: messageText}).then(function(result) {
  // Read result of the Cloud Function.
  var sanitizedMessage = result.data.text;
  // ...
});

Java

private Task<String> addMessage(String text) {
    // Create the arguments to the callable function.
    Map<String, Object> data = new HashMap<>();
    data.put("text", text);
    data.put("push", true);

    return mFunctions
            .getHttpsCallable("addMessage")
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    // This continuation runs on either success or failure, but if the task
                    // has failed then getResult() will throw an Exception which will be
                    // propagated down.
                    String result = (String) task.getResult().getData();
                    return result;
                }
            });
}

Kotlin

private fun addMessage(text: String): Task<String> {
    // Create the arguments to the callable function.
    val data = hashMapOf(
        "text" to text,
        "push" to true
    )

    return functions
            .getHttpsCallable("addMessage")
            .call(data)
            .continueWith { task ->
                // This continuation runs on either success or failure, but if the task
                // has failed then result will throw an Exception which will be
                // propagated down.
                val result = task.result?.data as String
                result
            }
}

C++

firebase::Future<firebase::functions::HttpsCallableResult> AddMessage(
    const string& text) {
  // Create the arguments to the callable function.
  firebase::Variant data = firebase::Variant::EmptyMap();
  data.map()["text"] = firebase::Variant(text);
  data.map()["push"] = true;

  // Call the function and add a callback for the result.
  firebase::functions::HttpsCallableReference doSomething =
      functions->GetHttpsCallable("addMessage");
  return doSomething.Call(data);
}

Unity

private Task<string> addMessage(string text) {
  // Create the arguments to the callable function.
  var data = new Dictionary<string, object>();
  data["text"] = text;
  data["push"] = true;

  // Call the function and extract the operation from the result.
  var function = functions.GetHttpsCallable("addMessage");
  return function.CallAsync(data).ContinueWith((task) => {
    return (string) task.Result.Data;
  });
}

在客户端上处理错误

如果服务器抛出错误或者生成的 Promise 被拒绝,则客户端会收到错误。

如果函数返回的错误类型为 function.https.HttpsError,那么客户端会从该服务器错误中收到错误的 codemessagedetails。否则,错误会包含消息 INTERNAL 和代码 INTERNAL。请参阅关于如何在 Callable 函数中处理错误的指南。

Swift

if let error = error as NSError? {
  if error.domain == FunctionsErrorDomain {
    let code = FunctionsErrorCode(rawValue: error.code)
    let message = error.localizedDescription
    let details = error.userInfo[FunctionsErrorDetailsKey]
  }
  // ...
}

Objective-C

if (error) {
  if (error.domain == FIRFunctionsErrorDomain) {
    FIRFunctionsErrorCode code = error.code;
    NSString *message = error.localizedDescription;
    NSObject *details = error.userInfo[FIRFunctionsErrorDetailsKey];
  }
  // ...
}

Web

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({text: messageText}).then(function(result) {
  // Read result of the Cloud Function.
  var sanitizedMessage = result.data.text;}).catch(function(error) {
  // Getting the Error details.
  var code = error.code;
  var message = error.message;
  var details = error.details;
  // ...
});

Java

addMessage(inputMessage)
        .addOnCompleteListener(new OnCompleteListener<String>() {
            @Override
            public void onComplete(@NonNull Task<String> task) {
                if (!task.isSuccessful()) {
                    Exception e = task.getException();
                    if (e instanceof FirebaseFunctionsException) {
                        FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                        FirebaseFunctionsException.Code code = ffe.getCode();
                        Object details = ffe.getDetails();
                    }

                    // ...
                }

                // ...
            }
        });

Kotlin

addMessage(inputMessage)
        .addOnCompleteListener(OnCompleteListener { task ->
            if (!task.isSuccessful) {
                val e = task.exception
                if (e is FirebaseFunctionsException) {
                    val code = e.code
                    val details = e.details
                }

                // ...
            }

            // ...
        })

C++

void OnAddMessageCallback(
    const firebase::Future<firebase::functions::HttpsCallableResult>& future) {
  if (future.error() != firebase::functions::kErrorNone) {
    // Function error code, will be kErrorInternal if the failure was not
    // handled properly in the function call.
    auto code = static_cast<firebase::functions::Error>(future.error());

    // Display the error in the UI.
    DisplayError(code, future.error_message());
    return;
  }

  const firebase::functions::HttpsCallableResult *result = future.result();
  firebase::Variant data = result->data();
  // This will assert if the result returned from the function wasn't a string.
  string message = data.string_value();
  // Display the result in the UI.
  DisplayResult(message);
}

// ...

// ...
  auto future = AddMessage(message);
  future.OnCompletion(OnAddMessageCallback);
// ...

Unity

 addMessage(text).ContinueWith((task) => {
  if (task.IsFaulted) {
    foreach (var inner in task.Exception.InnerExceptions) {
      if (inner is FunctionsException) {
        var e = (FunctionsException) inner;
        // Function error code, will be INTERNAL if the failure
        // was not handled properly in the function call.
        var code = e.ErrorCode;
        var message = e.ErrorMessage;
      }
    }
  } else {
    string result = task.Result;
  }
});