问题描述
我通过 functions.https.onRequest
创建了一个 Google Cloud Function,当我将它的 URL 粘贴到浏览器中并与我的 Firebase 设置很好地集成时,它运行良好.这个函数有点像从后端公开的 API 方法,我想从客户端调用它.在此特定实例中,客户端是一个 Android 应用程序.
I have created a Google Cloud Function through functions.https.onRequest
, which is working nicely when I paste its URL in a browser and integrates well with my Firebase setup. This function is meant as somewhat of an API method exposed from the backend, which I'd like to invoke from clients. In this particular instance, the client is an Android app.
有什么方法可以通过 Firebase 调用 Cloud Function 来执行 HTTP 请求?还是我仍需要执行手动 HTTP 请求?
Is there any way I can perform the HTTP request for the Cloud Function by invoking it through Firebase? Or will I have to still perform a manual HTTP request?
推荐答案
从12.0.0版本开始可以更简单的调用云函数
Since version 12.0.0 you can call cloud function in more simple way
在您的 build.gradle
implementation 'com.google.firebase:firebase-functions:19.0.2'
并使用以下代码
FirebaseFunctions.getInstance() // Optional region: .getInstance("europe-west1")
.getHttpsCallable("myCoolFunction")
.call(optionalObject)
.addOnFailureListener {
Log.wtf("FF", it)
}
.addOnSuccessListener {
toast(it.data.toString())
}
您可以安全地在主线程上使用它.回调也会在主线程上触发.
You can use it on main thread safely. Callbacks is triggered on main thread as well.
您可以在官方文档中阅读更多内容:https://firebase.google.com/docs/函数/可调用
You can read more in official docs: https://firebase.google.com/docs/functions/callable
这篇关于通过 Firebase 从 Android 调用云函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!