Firebase 函数调用返回"NOT_FOUND"异常。(安卓,科尔廷)



我正试图在我的koltin Android应用程序中使用Firebase函数将消息从客户端设备发送到另一个客户端设备。

index.js中的Firebase函数

exports.callUser = functions.https.onCall((data) => {
console.log('Call request received.');
// Message text passed from the client.
const registrationToken = data.registrationToken;
functions.logger.log('Calling', registrationToken);
var message = {
data: {
doctor: 'foo',
patient: 'bar',
room: 'foobar'
},
token: registrationToken
};
return admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent incoming call message:', response);
return "Sent";
})
.catch((error) => {
console.log('Error sending message:', error);
throw new functions.https.HttpsError('unknown', error.message, error);
});
});

Firebase函数客户端调用

private fun makeCall(registrationToken: String): Task<String> {
// Create the arguments to the callable function.
val data = hashMapOf(
"registrationToken" to registrationToken
)
Log.d(TAG, "callUser data input: $data")
return Firebase.functions
.getHttpsCallable("callUser")
.call(data)
.continueWith { task ->
val result = task.result?.data as String
result
}
}

来自函数的异常处理

makeCall(registrationToken)
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
val e = task.exception
if (e is FirebaseFunctionsException) {
val code = e.code
val details = e.details
Log.w(TAG, "$code")
}
// [START_EXCLUDE]
Log.w(TAG, "addMessage:onFailure", e)
showSnackbar("An error occurred.")
return@OnCompleteListener
// [END_EXCLUDE]
}
// [START_EXCLUDE]
val result = task.result
Log.d(TAG,"MakeCall result: $result")
val intent = Intent(activity, VideoActivity::class.java)
startActivity(intent)
// [END_EXCLUDE]
})

我已经能够编写简单的https.onRequest((函数,这些函数可以按预期工作,但我不知道这个回调函数做错了什么。

日志

W/HomeFragment: NOT_FOUND
W/HomeFragment: addMessage:onFailure
com.google.firebase.functions.FirebaseFunctionsException: NOT_FOUND
at com.google.firebase.functions.FirebaseFunctions$2.onResponse(com.google.firebase:firebase- 
functions@@19.0.2:281)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)

我正在使用Firebase模拟器进行测试,由于函数从未被成功调用,因此没有来自该模拟器的日志。

这是因为我使用的是Firebase Emulator。在访问"Firebase.functions"之前,必须包含"FirebaseFunctions.getInstance((.useFunctionsSimulator("http://10.0.2.2:5001"('如果您希望使用模拟器。

请参阅:https://firebase.google.com/docs/emulator-suite/connect_functions#callable_functions

最新更新