首页 > 解决方案 > 调用 Firebase Cloud Functions Android

问题描述

大家好,看这篇文章的人。

我有我的应用程序,我想知道如何在其中调用我的 Firebase Cloud Functions。我一直在阅读 Firebase 指南,但我真的很难理解它是如何工作的。

我有一个要创建聚会的功能,并且我有一些要插入的值,例如地址、日期、所有者等。

如果知道这方面的任何人可以帮助我,我将非常感激,我可以提供您可能需要的更多信息。谢谢!

标签: androidfirebasegoogle-cloud-functions

解决方案


正如弗兰克所说,当您在 StackOveflow 上提出问题时,最好包含您已经编写的所有代码。

但是,根据您的评论,我了解到您指的是文档中的代码片段(在下面复制/粘贴),并且您在data.put.

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

此 Java 代码片段显示传递(发送)到 Callable Cloud Function 的数据包含在HashMap名为data.

你会在网上找到很多关于如何使用 HashMap 的教程,但简而言之:

“Java HashMap 是 Java 的 Map 接口的基于哈希表的实现。您可能知道,Map 是键值对的集合。它将键映射到值。” 来源:https ://www.callicoder.com/java-hashmap/

将新的键值对添加到 HashMap 的一种方法是使用该put()方法。所以片段中的这部分代码是关于将数据添加到将发送到 CloudFunction 的 HashMap 中。

在 Cloud Function 中,您将获得如下数据(如文档中所述):

exports.addMessage = functions.https.onCall((data, context) => {
  // ...
  const text = data.text;
  const push = data.push;
  // ...
});

推荐阅读