Skip to content

Commit

Permalink
Merge pull request #6950 from RocketChat/fix-badge-counter-on-push-no…
Browse files Browse the repository at this point in the history
…tifications

[FIX] Fix badge counter on iOS push notifications
  • Loading branch information
rodrigok authored May 16, 2017
2 parents 5bf5c58 + 7903108 commit 7c88d8b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/rocketchat-lib/server/lib/PushNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`;
Expand All @@ -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,
Expand Down
43 changes: 27 additions & 16 deletions packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
});
});
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/rocketchat-lib/server/models/Subscriptions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 7c88d8b

Please sign in to comment.