Search code examples
node.jsfirebase-cloud-messagingfirebase-admin

Firebase Admin SDK sendMulticast


This code with a smaller list of tokens works correctly, but I don't know why it fails to send the notification to all the tokens when individually the token is valid.

I am doing something wrong? when the token list contains fewer tokens, all notifications are sent. There is a maximum of 30 tokens.

        let notificationData = {
            Id: messageInfo.ChatId,
            Type: notificationType.ChatMessage,
            Data: chatRoom
        };

        var payload = {
            notification: {
                title: title,
                body: body,
            },
            data: {
                NotificationData: JSON.stringify(notificationData),
            },
            apns: {
                payload: {
                    aps: {
                        sound: "default",
                    },
                },
            },
        };

        payload.tokens = chatRoom.FCMTokens;

        return admin.messaging().sendMulticast(payload).then(response => {
            if (response.failureCount > 0) {
                const failedTokens = [];
                response.responses.forEach((resp, idx) => {
                    if (!resp.success) {
                        failedTokens.push(payload.tokens[idx]);
                    }
                });
                console.log('List of tokens that caused failures: ' + JSON.stringify(response));
                console.log('List of tokens that caused failures: ' + failedTokens);
            }
            else {
                console.log("Successsfully MulticastMessage");
            }
            return null;
        }).catch(error => {
            console.log("Error sending notification", error);
            return null;
        });

more info:

enter image description here enter image description here


Solution

  • The problem was that being many users in the payload exceeded 4kb

    Notification messages can contain an optional data payload. Maximum payload for both message types is 4KB, except when sending messages from the Firebase console, which enforces a 1024 character limit.