【问题标题】:Call Firebase Cloud Functions Android调用 Firebase Cloud Functions Android
【发布时间】:2018-07-28 21:23:20
【问题描述】:

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

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

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

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

【问题讨论】:

  • Firebase 文档确实应该提供足够的信息来开始编写所谓的可调用云函数,然后从您的 Android 应用程序中调用它:firebase.google.com/docs/functions/callable#call_the_function。如果您无法使其正常工作,请展示您的尝试。没有看到这一点,任何人都很难比文档做得更好。
  • 我已经写了他们在指南中给你的“模板”,但我不明白的是任务。例如,我不知道将变量放在哪里,因为我不理解“data.put”,其中 data 是 hashmap,而在 put 方法中它有“text”和函数的字符串。

标签: android firebase google-cloud-functions


【解决方案1】:

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

但是,根据您的评论,我了解到您指的是 documentation 中的代码 sn-p(复制/粘贴在下面),并且您对 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 代码 sn-p 显示传递(发送)到 Callable Cloud Function 的数据包含在名为 dataHashMap 中。

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

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

向 HashMap 添加新的键值对的一种方法是使用 put() 方法。所以sn-p中的这部分代码是关于将数据添加到HashMap中,然后发送到CloudFunction。

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

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

【讨论】:

  • 如何将时间戳/日期时间作为数据传递?
  • @Adi 我建议您使用已经尝试过的代码创建一个新问题。
猜你喜欢
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 2018-06-11
相关资源
最近更新 更多