node.jsfirebase-cloud-messagingfirebase-admin

messaging.sendMulticast is not a function


I get the above error when I try and send messages to devices:

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

var serviceAccount = require("./configs.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://pushmessage-bd1eb.firebaseio.com"
});

const db = admin.firestore();
exports.getUsers = functions.https.onRequest(async (req, res) => {
    db.collection("users")
        .get()
        .then(snapshot => {
            const messaging = admin.messaging();
            let registrationTokens = [];
            snapshot.forEach(doc => {
                let id = doc.id;

                registrationTokens.push(id);
            });
            console.log(registrationTokens);

            // process the tokens
            const message = {
                data: { title: "Testing", body: "Test" },
                tokens: registrationTokens
            };

            messaging.sendMulticast(message).then(response => {
                console.log(
                    response.successCount + " messages were sent successfully"
                );
            });
        });
});

Solution

  • sendMulticast wasn't introduced into the Firebase Admin SDK until very recently. Try upgrading your firebase-admin dependency to the latest (npm install firebase-admin@latest).