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