Skip to content

Commit

Permalink
Step 16.9: Update addMessage Meteor method to send fcm notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Oct 16, 2017
1 parent e4b66ad commit 529f6ac
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api/server/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 529f6ac

Please sign in to comment.