【发布时间】:2020-07-21 13:33:13
【问题描述】:
我正在尝试在我的 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 模拟器进行测试,没有任何日志,因为该函数永远不会被成功调用。
【问题讨论】:
-
我已经在 node shell 中运行了 firebase 函数并且它可以工作。它只是从客户端应用程序调用它而不是。
标签: javascript firebase google-cloud-functions