node.js - Passing parameters to a cloud function in Flutter - Stack Overflow

I'm having an issue calling cloud functions through my flutter app. The function seems to be going

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,
        );
      }
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744218012a4563644.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信