简体   繁体   中英

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:

在此处输入图像描述 在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM