Search code examples
javascriptfluttergoogle-cloud-functionsfirebase-cloud-messaging

Flutter firebase cloud messaging bad request invalid arguments


I am trying to create a poke function with firebase functions and firebase cloud messaging in flutter. But i am getting bad request 400 invalid arguments error. I added log lines to my sendPoke.js file to check what is the problem. And the problem i am getting is sentBy and senTo is null. But i am sure sentTo or sentBy not null. can you help me? this is my sendPoke.js file

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

module.exports = async (data,context) => {
  const sentBy = context.auth?.uid; // Dürten kullanıcı
  const sentTo = data.sentTo;       // Dürtülen kullanıcı
  const message = data.message || "Seni dürttü!";
  const currentTime = admin.firestore.Timestamp.now();


  //I am giving error in here
  if (!sentBy || !sentTo) { 
    throw new functions.https.HttpsError(
      "invalid-argument",
      "requester and recever informations are necessary."
      
    );
  }
 await admin.messaging.sendToDevice(sentTo, payload);//send a notifications to user

and this is how i am calling this function in dart :

Future<void> sendPokeNotification(String sentTo) async {
  try {
    // call callable function
    final HttpsCallable callable =
        FirebaseFunctions.instance.httpsCallable('sendPoke');

    print('senTo '+sentTo);//this is not null
    final currentUser = FirebaseAuth.instance.currentUser;
if (currentUser == null) {
  throw Exception("The user did not login.");
}

final idToken = await currentUser.getIdToken();
print("UserID Token: $idToken");
    // send data and give respond
    final response = await callable.call(<String, dynamic>{
      "sentTo": sentTo, // reciever UID
      "message": 'Hi, i poked you!', // message content
    });

    print('Function respond: ${response.data}');
  } catch (e) {
    print('Send Poke Error: $e');
  }
}

sentBy is sender uid and sentTo is reciever device Token. Also i added sendPoke function to idnex.js file with this way :

const sendPoke = require("./functions/utilities/pokeAndNotifications/sendPoke");
exports.sendPoke = functions.https.onCall(sendPoke);

Solution

  • You're invoking a so-called Callable Cloud Function from your Flutter code, but your JavaScript implement a regular HTTPS Cloud Function. The two types are not interoperable.

    You will either have to implement a Callable Cloud Function in your JavaScript, or use a regular HTTP(S) call (rather than the Callable wrapper from the Firebsae SDK) in your Flutter code.