Skip to content

Commit

Permalink
Adds payload data to persistence (#467)
Browse files Browse the repository at this point in the history
Fixes #466

Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan authored Jun 30, 2024
1 parent 48e37d7 commit 0491073
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions models/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
});

Expand Down
12 changes: 7 additions & 5 deletions notificationsender/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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()
};

Expand Down
17 changes: 14 additions & 3 deletions notificationsender/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion routes/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 0491073

Please sign in to comment.