admin管理员组文章数量:1399754
I'm having an issue calling cloud functions through my flutter app. The function seems to be going through according to the logs, but the values I pass in through Flutter are not making it to the function and the logs are telling me that the variables I passed in such as FCMtoken, title, and body are all empty.
Here is my Flutter code:
Future<void> sendNotification({
required String fcmToken,
required String title,
required String body,
}) async {
final HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('sendNotification');
try {
print("Sending notification with data:");
print("FCM Token: $fcmToken");
print("Title: $title");
print("Body: $body");
// Send data directly without nesting
final result = await callable.call({
'fcmToken': fcmToken,
'title': title,
'body': body,
});
} catch (e) {
print('Error sending notification: $e');
}
}
And here is my cloud function index.js:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.sendNotification = functions.https.onCall(async (data, context) => {
// Extract data directly (no nesting)
const {fcmToken, title, body} = data;
// Safe logging without circular references
console.log("Received notification request:", {
fcmToken: fcmToken || "not provided",
title: title || "not provided",
body: body || "not provided",
});
// Validate required fields
if (!fcmToken) {
throw new functions.https.HttpsError(
"invalid-argument",
"FCM Token is required",
);
}
// Construct the message payload
const message = {
token: fcmToken,
notification: {
title: title || "New Message",
body: body || "You have a new message",
},
};
try {
const response = await admin.messaging().send(message);
console.log("Successfully sent message:", response);
return {success: true, messageId: response};
} catch (error) {
console.error("Error sending message:", error.message);
throw new functions.https.HttpsError(
"internal",
"Failed to send notification: " + error.message,
);
}
});
本文标签: nodejsPassing parameters to a cloud function in FlutterStack Overflow
版权声明:本文标题:node.js - Passing parameters to a cloud function in Flutter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744218011a2595728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论