Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix badge counter on iOS push notifications #6950

Merged
merged 1 commit into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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