diff --git a/keyserver/src/push/utils.js b/keyserver/src/push/utils.js --- a/keyserver/src/push/utils.js +++ b/keyserver/src/push/utils.js @@ -20,9 +20,11 @@ 'messaging/registration-token-not-registered', 'messaging/invalid-registration-token', ]); +const fcmMaxMessagePayloadByteSize = 4000; const apnTokenInvalidationErrorCode = 410; const apnBadRequestErrorCode = 400; const apnBadTokenErrorString = 'BadDeviceToken'; +const apnMaxMessagePayloadByteSize = 4096; type APNPushResult = | { +success: true } @@ -151,6 +153,7 @@ options: Object, ) { try { + validateAndroidNotificationByteSize(notification); const deliveryResult = await provider .messaging() .sendToDevice(deviceToken, notification, options); @@ -196,4 +199,24 @@ return usersToUnreadCounts; } -export { apnPush, fcmPush, getUnreadCounts }; +function validateAndroidNotificationByteSize(notification: Object) { + const notificationByteSizeExceeded = + Buffer.byteLength(JSON.stringify(notification)) > + fcmMaxMessagePayloadByteSize; + if ( + notificationByteSizeExceeded && + 'custom_notification' in notification.data + ) { + delete notification.data.custom_notification.messageInfos; + } else if (notificationByteSizeExceeded) { + delete notification.data.messageInfos; + } +} + +export { + apnPush, + fcmPush, + getUnreadCounts, + apnMaxMessagePayloadByteSize, + fcmMaxMessagePayloadByteSize, +};