skip to Main Content

I call my function when I do an action somewhere in a flutter class. (when a user add a word, I want the partner connected with receive the notification about the new word!)
When I print partnerFcmToken, it’s well here and working well.
Problem is that when I call the function, it always return the token is undefined.

String? partnerFcmToken = userSnap['partner_fcm_token'] as String?;

if (partnerFcmToken != null && partnerFcmToken.isNotEmpty) {
  print("Envoie notif fait.");
  print(partnerFcmToken);

  // Call the cloud function to send the notification
  final response = await FirebaseFunctions.instance
      .httpsCallable('sendNotificationToPartnerPhoneV2')
      .call({
     'token_partner': partnerFcmToken,
    'lang1': lang1, // Include lang1 in the request
    'lang2': lang2, // Include lang2 in the request
  });

  if (response.data['success']) {
    print("Notification sent successfully!");
  } else {
    print("Failed to send notification: ${response.data['error']}");
  }
}

And here is the cloud function called.
It return Partner FCM token is invalid or empty.

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

// eslint-disable-next-line max-len
exports.sendNotificationToPartnerPhoneV2 = functions.https.onCall(async (request) => {
  console.log("Starting sendNotificationToPartnerPhoneV2 function");

  // Retrieve the FCM token from the request
  const partnerFcmToken = request.token_partner;

  // Validate the FCM token
  if (typeof partnerFcmToken !== "string" || partnerFcmToken.trim() === "") {
    console.error("Invalid partner FCM token:", partnerFcmToken);
    return {success: false, error: "Partner FCM token is invalid or empty."};
  }

  // Prepare the notification message
  const message = {
    notification: {
      title: "Your partner added a new word!",
      // eslint-disable-next-line max-len
      body: `${request.lang1} / ${request.lang2}`, // Use template literals for message body
    },
    token: partnerFcmToken,
  };

  // Log the message to be sent
  console.log("Message to be sent:", JSON.stringify(message, null, 2));

  try {
    // Send the notification
    await admin.messaging().send(message);
    console.log("Notification sent successfully.");
    return {success: true};
  } catch (error) {
    console.error("Error sending notification:", error);
    return {success: false, error: error.message};
  }
});

I guess I call it badly the ‘request’ or data that I put in parameters of the function, but I have no idea how it works in JS properly.

Thanks already for your help !
MG

2

Answers


  1. Chosen as BEST ANSWER

    OK I fixed it : The "Calling functions from your application" I used the 1st generation, I just changed to 2nd generation and it works perfectly now. No idea why they still propose the 1st.

    https://firebase.google.com/docs/functions/callable?gen=2nd&hl=fr#node.js_2


  2. According to the documentation, the object payload you send from the client appears in the data property of the CallableRequest object in the function.

    The request parameter contains data passed from the client app as well as additional context like authentication state. For a callable function that saves a text message to the Realtime Database, for example, data could contain the message text, along with auth information in auth.

    So your code would look like this:

      const partnerFcmToken = request.data.token_partner;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search