简体   繁体   English

呼叫Firebase Cloud Functions Android

[英]Call Firebase Cloud Functions Android

Hi to everyone watching this post. 大家看这个帖子的大家好

I have my app, and I want to know how to call my Firebase Cloud Functions in it. 我有我的应用程序,我想知道如何在其中调用Firebase Cloud Functions。 I've been reading from the Firebase guides but I am really struggling to understand how it works. 我一直在阅读Firebase指南,但是我真的很难理解它的工作原理。

I have a function where I want to create parties, and I have some values to be inserted such as address, date, owner, etc. 我有一个要在其中创建聚会的函数,并且要插入一些值,例如地址,日期,所有者等。

If anyone who knows about this can help me I would be really grateful, I can provide any more information that you could need. 如果知道这一点的任何人可以帮助我,我将非常感激,我可以提供您可能需要的更多信息。 Thanks! 谢谢!

As Frank said, it is better when you ask a question on StackOveflow to include all the code you have already written. 正如Frank所说,最好是在StackOveflow上问一个问题,以包括已经编写的所有代码。

However, from your comment, I understand you are referring to the code snippet that is in the documentation (copied/pasted below), and that you have problems with data.put . 但是,从您的评论中,我了解到您是在参考文档中的代码片段(在下面复制/粘贴),并且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;
                }
            });
}

This Java code snippet shows that the data that is passed (sent) to the Callable Cloud Function is contained in an HashMap , named data . 此Java代码段显示了传递(发送)到Callable Cloud Function的data包含在名为dataHashMap

You will find a lot of tutorials on the web on how to use an HashMap, but in a nutshell: 简而言之,您会在网上找到很多有关如何使用HashMap的教程。

"A Java HashMap is a hash table based implementation of Java's Map interface. A Map, as you might know, is a collection of key-value pairs. It maps keys to values." “ Java HashMap是Java的Map接口基于哈希表的实现。您可能知道,Map是键-值对的集合。它将键映射到值。” Source: https://www.callicoder.com/java-hashmap/ 资料来源: https : //www.callicoder.com/java-hashmap/

A way to add new key-value pairs to the HashMap is by using the put() method. 将新的键值对添加到HashMap的一种方法是使用put()方法。 So this part of the code in the snippet is about adding data to the HashMap that will be sent to the CloudFunction. 因此,该代码段中的这部分代码是关于将数据添加到HashMap中,该数据将发送到CloudFunction。

And in the Cloud Function you will get this data as follows (as explained in the doc): 在Cloud Function中,您将按以下方式获取此数据(如文档中所述):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM