Permalink
Browse files

Step 16.9: Update addMessage Meteor method to send fcm notifications

  • Loading branch information...
darkbasic committed Jun 8, 2017
1 parent cfe3200 commit c1ae3a658f0ed2f7e1e9ad9d3d3839cd6fce050e
Showing with 16 additions and 0 deletions.
  1. +16 −0 api/server/methods.ts
View
@@ -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,

0 comments on commit c1ae3a6

Please sign in to comment.