diff --git a/keyserver/src/push/send.js b/keyserver/src/push/send.js --- a/keyserver/src/push/send.js +++ b/keyserver/src/push/send.js @@ -45,6 +45,7 @@ fcmPush, getUnreadCounts, apnMaxNotificationPayloadByteSize, + fcmMaxNotificationPayloadByteSize, } from './utils'; type Device = { @@ -530,33 +531,38 @@ allMessageInfos, threadInfo, ); - const messageInfos = JSON.stringify(newRawMessageInfos); - - if (badgeOnly && codeVersion < 69) { - // Older Android clients don't look at badgeOnly, so if we sent them the - // full payload they would treat it as a normal notif. Instead we will - // send them this payload that is missing an ID, which will prevent the - // system notif from being generated, but still allow for in-app notifs - // and badge updating. - return { - data: { - badge: unreadCount.toString(), - ...rest, - threadID: threadInfo.id, - messageInfos, - }, - }; - } - return { + const notification = { data: { badge: unreadCount.toString(), ...rest, - id: notifID, threadID: threadInfo.id, - messageInfos, - badgeOnly: badgeOnly ? '1' : '0', }, }; + if (!badgeOnly || codeVersion >= 69) { + notification.data = { + ...notification.data, + id: notifID, + badgeOnly: badgeOnly ? '1' : '0', + }; + } + // Older Android clients don't look at badgeOnly, so if we sent them the + // full payload they would treat it as a normal notif. Instead we will + // send them a payload that is missing an ID, which will prevent the + // system notif from being generated, but still allow for in-app notifs + // and badge updating. + const messageInfos = JSON.stringify(newRawMessageInfos); + const copyWithMessageInfos = { + ...notification, + data: { ...notification.data, messageInfos }, + }; + + if ( + Buffer.byteLength(JSON.stringify(copyWithMessageInfos)) <= + fcmMaxNotificationPayloadByteSize + ) { + return copyWithMessageInfos; + } + return notification; } type NotificationInfo =