diff --git a/api/server/methods.ts b/api/server/methods.ts index a75640950..4b317fb1e 100644 --- a/api/server/methods.ts +++ b/api/server/methods.ts @@ -3,6 +3,7 @@ import { Messages } from './collections/messages'; import { MessageType, Profile } from './models'; import { check, Match } from 'meteor/check'; import { Users } from "./collections/users"; +import { fcmService } from "./services/fcm"; const nonEmptyString = Match.Where((str) => { check(str, String); @@ -83,6 +84,21 @@ Meteor.methods({ 'Chat doesn\'t exist'); } + const userId = this.userId; + const senderName = Users.collection.findOne({_id: userId}).profile.name; + const memberIds = Chats.collection.findOne({_id: chatId}).memberIds; + const tokens: string[] = Users.collection.find( + { + _id: {$in: memberIds, $nin: [userId]}, + fcmToken: {$exists: true} + } + ).map((el) => el.fcmToken); + + for (let token of tokens) { + console.log("Sending FCM notification"); + fcmService.sendNotification({"title": `New message from ${senderName}`, "text": content}, token); + } + return { messageId: Messages.collection.insert({ chatId: chatId,