我正在尝试在 Kotlin Android 应用程序中使用 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);
});
});
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 模拟器。如果您想使用模拟器,在访问“Firebase.functions”之前,必须包含“FirebaseFunctions.getInstance().useFunctionsEmulator("http://10.0.2.2:5001")”。
请参阅:https://firebase.google.com/docs/emulator-suite/connect_functions#callable_functions