From 289e8e5b3532de27cb95410bfd016cc9173e1746 Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Sat, 29 Jun 2024 16:26:48 -0700 Subject: [PATCH] Adds payload data to persistence Fixes #466 Signed-off-by: Dan Cunningham --- models/notification.js | 4 ++++ notificationsender/firebase.js | 12 +++++++----- notificationsender/index.js | 17 ++++++++++++++--- routes/devices.js | 2 +- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/models/notification.js b/models/notification.js index f473a4c..9820840 100644 --- a/models/notification.js +++ b/models/notification.js @@ -8,6 +8,10 @@ var NotificationSchema = new Schema({ icon: String, severity: String, acknowledged: Boolean, + payload: { + type: Schema.Types.Mixed, + default: {} + }, created: { type: Date, default: Date.now, expires: '30d' } }); diff --git a/notificationsender/firebase.js b/notificationsender/firebase.js index 8f1e30a..8bbdb62 100644 --- a/notificationsender/firebase.js +++ b/notificationsender/firebase.js @@ -21,12 +21,14 @@ function sendMessage(message) { }); }; -exports.sendFCMNotification = function (registrationIds, notificationId, data) { +exports.sendFCMNotification = function (registrationIds, notification) { // We can safely remove androidNotificationId, our android client has removed the need for this, but i need to double check redis.incr("androidNotificationId", function (error, androidNotificationId) { if (error) { return; } + const data = notification.payload; + //for IOS we need to set an actual notification payload so they show up when the app is not running //we can remove badge/sound/alert after our IOS app dynamically adds these const apns = { @@ -46,10 +48,10 @@ exports.sendFCMNotification = function (registrationIds, notificationId, data) { const updatedData = { type: 'notification', - severity: data.severity || '', - icon: data.icon || '', - persistedId: notificationId.toString(), - timestamp: Date.now().toString(), + severity: notification.severity || '', + icon: notification.icon || '', + persistedId: notification._id.toString(), + timestamp: notification.created.toString(), notificationId: androidNotificationId.toString() }; diff --git a/notificationsender/index.js b/notificationsender/index.js index 75f5333..c6fc079 100644 --- a/notificationsender/index.js +++ b/notificationsender/index.js @@ -3,16 +3,27 @@ const UserDevice = require('../models/userdevice'); const logger = require('../logger'); const firebase = require('./firebase'); const aps = require("./aps-helper") +const maxSizeInBytes = 1048576; //1MB function sendNotification(userId, data) { return new Promise((resolve, reject) => { + + const jsonString = JSON.stringify(data); + const jsonSizeInBytes = Buffer.byteLength(jsonString, 'utf8'); + + // Check if the JSON size exceeds the limit + if (jsonSizeInBytes > maxSizeInBytes) { + reject(`JSON data exceeds the maximum allowed size of ${maxSizeInBytes} bytes.`) + } + var fcmRegistrations = []; var iosDeviceTokens = []; var newNotification = new Notification({ user: userId, message: data.message, icon: data.icon, - severity: data.severity + severity: data.severity, + payload: data }); newNotification.save(function (error) { if (error) { @@ -35,10 +46,10 @@ function sendNotification(userId, data) { iosDeviceTokens.push(device.iosDeviceToken); } }); - + // If we found any FCM devices, send notification if (fcmRegistrations.length > 0) { - firebase.sendFCMNotification(fcmRegistrations, newNotification._id, data); + firebase.sendFCMNotification(fcmRegistrations, newNotification); } // If we found any ios devices, send notification diff --git a/routes/devices.js b/routes/devices.js index 53df91b..246f282 100644 --- a/routes/devices.js +++ b/routes/devices.js @@ -62,7 +62,7 @@ exports.devicessendmessage = function (req, res) { UserDevice.findOne({ owner: req.user.id, _id: sendMessageDeviceId }, function (error, sendMessageDevice) { if (!error && sendMessageDevice) { if (sendMessageDevice.fcmRegistration) { - notificationSender.sendFCMNotification(sendMessageDevice.fcmRegistration, newNotification._id, { message: message }); + notificationSender.sendFCMNotification(sendMessageDevice.fcmRegistration, newNotification); } else if (sendMessageDevice.iosDeviceToken) { notificationSender.sendAppleNotification(sendMessageDevice.iosDeviceToken, message); }