From 529f6acb78018faf6ea0b1d062fa3f5b20def3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Belli?= Date: Thu, 8 Jun 2017 23:33:53 +0200 Subject: [PATCH] Step 16.9: Update addMessage Meteor method to send fcm notifications --- api/server/methods.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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,