Skip to content

Commit 529f6ac

Browse files
committed
Step 16.9: Update addMessage Meteor method to send fcm notifications
1 parent e4b66ad commit 529f6ac

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

api/server/methods.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Messages } from './collections/messages';
33
import { MessageType, Profile } from './models';
44
import { check, Match } from 'meteor/check';
55
import { Users } from "./collections/users";
6+
import { fcmService } from "./services/fcm";
67

78
const nonEmptyString = Match.Where((str) => {
89
check(str, String);
@@ -83,6 +84,21 @@ Meteor.methods({
8384
'Chat doesn\'t exist');
8485
}
8586

87+
const userId = this.userId;
88+
const senderName = Users.collection.findOne({_id: userId}).profile.name;
89+
const memberIds = Chats.collection.findOne({_id: chatId}).memberIds;
90+
const tokens: string[] = Users.collection.find(
91+
{
92+
_id: {$in: memberIds, $nin: [userId]},
93+
fcmToken: {$exists: true}
94+
}
95+
).map((el) => el.fcmToken);
96+
97+
for (let token of tokens) {
98+
console.log("Sending FCM notification");
99+
fcmService.sendNotification({"title": `New message from ${senderName}`, "text": content}, token);
100+
}
101+
86102
return {
87103
messageId: Messages.collection.insert({
88104
chatId: chatId,

0 commit comments

Comments
 (0)