diff --git a/packages/rocketchat-lib/server/lib/PushNotification.js b/packages/rocketchat-lib/server/lib/PushNotification.js index fd1bdb8db604..fca87e6455a8 100644 --- a/packages/rocketchat-lib/server/lib/PushNotification.js +++ b/packages/rocketchat-lib/server/lib/PushNotification.js @@ -16,7 +16,7 @@ class PushNotification { return hash; } - send({ roomName, roomId, username, message, usersTo, payload }) { + send({ roomName, roomId, username, message, usersTo, payload, badge = 1 }) { let title; if (roomName && roomName !== '') { title = `${ roomName }`; @@ -27,7 +27,7 @@ class PushNotification { const icon = RocketChat.settings.get('Assets_favicon_192').url || RocketChat.settings.get('Assets_favicon_192').defaultUrl; const config = { from: 'push', - badge: 1, + badge, sound: 'default', title, text: message, diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index ea6ef470118a..b4802e357f85 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -1,6 +1,14 @@ /* globals Push */ import moment from 'moment'; +function getBadgeCount(userId) { + const subscriptions = RocketChat.models.Subscriptions.findUnreadByUserId(userId).fetch(); + + return subscriptions.reduce((unread, sub) => { + return sub.unread + unread; + }, 0); +} + RocketChat.callbacks.add('afterSaveMessage', function(message, room) { // skips this callback if the message was edited if (message.editedAt) { @@ -152,6 +160,7 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { roomId: message.rid, username: push_username, message: push_message, + badge: getBadgeCount(userOfMention._id), payload: { host: Meteor.absoluteUrl(), rid: message.rid, @@ -299,23 +308,25 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { if (userIdsToPushNotify.length > 0) { if (Push.enabled === true) { - RocketChat.PushNotification.send({ - roomId: message.rid, - roomName: push_room, - username: push_username, - message: push_message, - payload: { - host: Meteor.absoluteUrl(), - rid: message.rid, - sender: message.u, - type: room.t, - name: room.name - }, - usersTo: { - userId: { - $in: userIdsToPushNotify + // send a push notification for each user individually (to get his/her badge count) + userIdsToPushNotify.forEach((userIdToNotify) => { + RocketChat.PushNotification.send({ + roomId: message.rid, + roomName: push_room, + username: push_username, + message: push_message, + badge: getBadgeCount(userIdToNotify), + payload: { + host: Meteor.absoluteUrl(), + rid: message.rid, + sender: message.u, + type: room.t, + name: room.name + }, + usersTo: { + userId: userIdToNotify } - } + }); }); } } diff --git a/packages/rocketchat-lib/server/models/Subscriptions.coffee b/packages/rocketchat-lib/server/models/Subscriptions.coffee index 95f12bff51ff..2eb4f13d484a 100644 --- a/packages/rocketchat-lib/server/models/Subscriptions.coffee +++ b/packages/rocketchat-lib/server/models/Subscriptions.coffee @@ -126,6 +126,14 @@ class ModelSubscriptions extends RocketChat.models._Base return @find query + findUnreadByUserId: (userId) -> + query = + 'u._id': userId + unread: + $gt: 0 + + return @find query, fields: unread: 1 + # UPDATE archiveByRoomId: (roomId) -> query =