简体   繁体   中英

How to return data from cloud function to android and use it?

How can I get data from Cloud onCall() function to Android client and use it in a java code?

The client connects to function and function connects to firestore. But I can`t get data back from the function to client. I know that data has json format. But how to pass data to android, and how to use this data in android code (for example to setText in TextView).

java onClick() method:

onClick(){
String first = editTextTitle.getText().toString();
String second = editTextDescription.getText().toString();

//Here I try to setText received from function, but nothing happen.
tv_function.setText(function_3(first, second).toString());
}

java - call function:

private Task<String> function_3(String first, String second) {
    mFunctions = FirebaseFunctions.getInstance();        
    Map<String, Object> data = new HashMap<>();
    data.put("text", first);
    data.put("push", second);

    return mFunctions
            .getHttpsCallable("sendMessageToAndroid")
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() 
{
     @Override
     public String then(@NonNull Task<HttpsCallableResult> task) throws 
Exception {
     String result = (String)task.getResult().getData();
     return result;
     }
   });
}

javaScript function:

exports.sendMessageToAndroid = functions.https.onCall((data, context) => {
var batono = {
    first: data.fara,
    second: data.mina
}

return db.collection('abra-codabra').doc("mu-mu").set(batono)
.then(()=>{
    var arba = {
        aram:"aramando",
        borem:"boremuno"
    }
    return arba;
});
});

How to get var "arba" to AndroidStudio and set it to TextView?

By doing

Map<String, Object> data = new HashMap<>();
    data.put("text", first);
    data.put("push", second);

    return mFunctions
            .getHttpsCallable("sendMessageToAndroid")
            .call(data)
            ...

(as shown in the example from the documentation ) you send to your Callable Cloud Function a map with a text element.

However, in your Cloud Function code you do:

var batono = {
    first: data.fara,
    second: data.mina
}

So, it means that you should send a map with the following elements: fara and mina , not a map with text (or you would have done something like var batono = {text: data.text} ).

So, you should most probably do something like the following (not tested however):

Map<String, Object> data = new HashMap<>();
    data.put("fara", .....);
    data.put("mina", .....);
    data.put("push", true);

    return mFunctions
            .getHttpsCallable("sendMessageToAndroid")
            .call(data)
            ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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