Skip to content
Snippets Groups Projects
Unverified Commit 79031084 authored by Diego Sampaio's avatar Diego Sampaio
Browse files

Fix badge counter on iOS push notifications

Closes #6356
parent f8c8633b
No related merge requests found
......@@ -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,
......
/* 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
}
}
});
});
}
}
......
......@@ -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 =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment