Calling a Cloud Function from Android through Firebase

前端 未结 4 1146
花落未央
花落未央 2020-12-25 14:53

Situation

I have created a Google Cloud Function through functions.https.onRequest, which is working nicely when I paste its URL in a browser and integ

4条回答
  •  心在旅途
    2020-12-25 15:31

    Yes it's possible :

    1. Add this to app/build.gradle file :

      implementation 'com.google.firebase:firebase-functions:16.1.0'


    1. Initialize the client SDK

      private FirebaseFunctions mFunctions;

      mFunctions = FirebaseFunctions.getInstance();


    1. Call the function

      private Task addMessage(String text) {
      
      Map data = new HashMap<>();
      data.put("text", text);
      data.put("push", true);
      
      return mFunctions
              .getHttpsCallable("addMessage")
              .call(data)
              .continueWith(new Continuation() {
                  @Override
                  public String then(@NonNull Task 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;
                  }
              });
         }
      

    Ref : Calling Firebase cloud functions

提交回复
热议问题