1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter / Cloud Messaging notification - the token is undefined when calling...

Discussão em 'Mobile' iniciado por Stack, Outubro 25, 2024 às 13:42.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página