I follow this course: https://firebase.google.com/docs/functions/callable#java_2 and i can't call my function. However i can call triggered functions:
In index.json
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
//firebase.functions().useFunctionsEmulator('http://localhost:5000') I don't know if i need to use that
//and don't find the path of the firebase module
exports.addMessage = functions.https.onCall((data, context) => {
console.log("addMessage call");
const text = data.text;
const uid = context.auth.uid;
const name = context.auth.token || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;
// Checking attribute.
if (!(typeof text === 'string') || text.length === 0) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
'one arguments "text" containing the message text to add.');
}
// Checking that the user is authenticated.
if (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
}
// Saving the new message to the Realtime Database.
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
text: sanitizedMessage,
author: { uid, name, picture, email },
}).then(() => {
console.log('New Message written');
// Returning the sanitized message to the client.
return { text: sanitizedMessage };
})
});
In Android Studio (java) :
public class MainActivity extends AppCompatActivity {
private final static String TAG = "DebugTest";
private DatabaseReference mRef ;
private FirebaseFunctions mFunctions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clickBtn = findViewById(R.id.clickBtn);
// WARNING: firebase emulators
mRef = FirebaseDatabase
.getInstance("http://10.0.2.2:9000?ns=my-firebase-application-xxxxx")
.getReference();
mFunctions = FirebaseFunctions.getInstance("http://10.0.2.2:9000?ns=my-firebase-application-xxxx");
clickBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addMessage("salut les copains")
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
Log.d(TAG,"onComplete call");
if (!task.isSuccessful()) {
Exception e = task.getException();
if (e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
FirebaseFunctionsException.Code code = ffe.getCode();
Object details = ffe.getDetails();
Log.d(TAG,"problème dans appelle de la fonction addMessage: \n " + code.toString());
}
// ...
}
// ...
}
});
}
});
}
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();
Log.d(TAG, "result going to be send");
return result;
}
});
}
}
I have this in my Android console:
D/DebugTest: onComplete call
D/DebugTest: problème dans appelle de la fonction addMessage:
INTERNAL
So then (in ContinueWith) don't work or is not call.
And I don't have any log like "addMessage call" in firebase emulator...
Someone have any Idea.
PS: I don't know how to find the module to call firebase.functions().useFunctionsEmulator('http://localhost:5000')