通过Firebase从Android调用云函数 [英] Calling a Cloud Function from Android through Firebase

查看:125
本文介绍了通过Firebase从Android调用云函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过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/functions/callable

You can read more in official docs: https://firebase.google.com/docs/functions/callable

这篇关于通过Firebase从Android调用云函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆