'FirebaseError: Messaging: This browser doesn't support the API's required to use the Firebase SDK. (messaging/unsup ported-browser)

I am using Realtime database, cloud functions and cloud messaging services from Firebase to code my app on Android Studio, I wrote a cloud function in my index.js:

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

const app = admin.initializeApp();
const messaging = mess.getMessaging(app);


// Listener method
// Route pour envoyer une notification à tous les utilisateurs intéressés
// par le type de produit MODIFIE
exports.sendListenerPushNotification = functions.database
    .ref("Products/{product}/type")
    .onUpdate((snapshot, context) => {
      // on récupère le type du produit
      const type = snapshot.after.val();
      // on récupère les utilisateurs interresse par le type d'objet
      const users = admin.database().ref("Notif/type/" + type)
          .once("value").then((snapshot) => {
            return snapshot.val();
          }).catch((error) => {
            console.log("Error sending message:", error);
            return false;
          });
      // Retourne les utilisateurs à notifier et l'état de la requête
      console.log("users est une promise");
      const result = {state: "update", users: users};
      console.log("Le résultat final est: " + JSON.stringify(result));
      const FCMToken = messaging.getToken({
        vapidKey: "BLyOjzWbfJkDZeaRYItAYJ5piZV-IY15bq0jnczscrKtbKZY"+
        "-j2WQiiLabbQwEjOFp9dDfj1xEVroFc-nz6LKsM",
      });
      const message = "Un nouvel objet de type: " + type + " est disponible";
      const payload = {
        token: FCMToken,
        notification: {
          title: "Nouveau produit disponible",
          body: message,
        },
        data: {
          body: message,
        },
      };
      admin.messaging().send(payload).then((response) => {
        console.log("Message envoyé: " + response);
        return {success: true};
      });
    });

I want to push a notification from this file to be interpreted by my cloud messaging service in Firebase, I already have coded everything in my MainActivity and my FirebaseMessagingService class but I got this error: error from terminal

I searched but I don't know what to use for Android

I resolved the package.json problem btw

Thanks for help !



Solution 1:[1]

I resolved the problem.

For an app build with Android Studio you have to write this code instead of the first I posted:

  return admin.messaging().sendToTopic(
      "Produit",
      {
        notification: {
          title: "Tier Chaser",
          body: message,
        },
      });

What you write in "Produit", is the name of your topic you can get in your activity in java:

FirebaseMessaging.getInstance().subscribeToTopic("Produit")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Inv4si0n