Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add body data on firebase notification #236

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/services/fcm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ export class FCMService {

async sendNotification(
fcmTokens?: string[],
title?: string,
body?: string,
data?: AnyObject,
): Promise<void> {
if (fcmTokens == null || fcmTokens.length <= 0) return;

const message = {
tokens: fcmTokens,
data: data,
notification: {
title: title,
body: body,
},
};

await firebaseAdmin.messaging().sendMulticast(message);
Expand Down
97 changes: 92 additions & 5 deletions src/services/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ export class NotificationService {
);
if (createdNotification === null) return false;

const title = 'Friend Request Accepted';
const body = fromUser.name + ' ' + notification.message;
await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);

Expand All @@ -102,8 +106,12 @@ export class NotificationService {
);
if (createdNotification === null) return false;

const title = 'Friend Request Accepted';
const body = fromUser.name + ' ' + notification.message;
await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);

Expand Down Expand Up @@ -146,6 +154,10 @@ export class NotificationService {
notification.referenceId = comment.id;
notification.message = 'commented: ' + comment.text;

// FCM messages
const title = 'New Comment';
const body = fromUser.name + ' ' + notification.message;

let toUser: User = new User();

const post = await this.postRepository.findById(comment.postId);
Expand Down Expand Up @@ -176,6 +188,8 @@ export class NotificationService {
);
await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);
}
Expand Down Expand Up @@ -245,7 +259,12 @@ export class NotificationService {
const found = createdNotifications.find(notif => notif.to === to.id);

if (found) {
return this.fcmService.sendNotification(to.fcmTokens, found);
return this.fcmService.sendNotification(
to.fcmTokens,
title,
body,
found,
);
}

return;
Expand Down Expand Up @@ -329,7 +348,12 @@ export class NotificationService {
const found = createdNotifications.find(notif => notif.to === to.id);

if (found) {
return this.fcmService.sendNotification(to.fcmTokens, found);
return this.fcmService.sendNotification(
to.fcmTokens,
'Report Approved',
'Myriad Official ' + notification.message,
found,
);
}

return;
Expand Down Expand Up @@ -373,6 +397,8 @@ export class NotificationService {
);
await this.fcmService.sendNotification(
commentUser.fcmTokens,
'Comment Removed',
'Myriad Official ' + notification.message,
createdNotification,
);

Expand Down Expand Up @@ -438,7 +464,12 @@ export class NotificationService {
);

if (found) {
return this.fcmService.sendNotification(to.fcmTokens, found);
return this.fcmService.sendNotification(
to.fcmTokens,
'Post Removed',
'Myriad Official ' + notification.message,
found,
);
}

return;
Expand All @@ -461,6 +492,8 @@ export class NotificationService {
);
await this.fcmService.sendNotification(
user.fcmTokens,
'User Removed',
'Myriad Official ' + notification.message,
createdNotification,
);
break;
Expand All @@ -485,6 +518,10 @@ export class NotificationService {
notification.referenceId = vote.id;
notification.message = vote.state ? 'upvoted' : 'downvoted';

// FCM messages
const title = 'New Vote';
const body = fromUser.name + ' ' + notification.message;

let toUser = null;

// Notification vote to comment
Expand All @@ -502,6 +539,8 @@ export class NotificationService {

await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);

Expand Down Expand Up @@ -560,7 +599,12 @@ export class NotificationService {
const found = createdNotifications.find(notif => notif.to === to.id);

if (found) {
return this.fcmService.sendNotification(to.fcmTokens, found);
return this.fcmService.sendNotification(
to.fcmTokens,
title,
body,
found,
);
}
}),
);
Expand Down Expand Up @@ -591,6 +635,10 @@ export class NotificationService {
await this.getCommentAdditionalReferenceIds(to);
}

// FCM messages
const title = 'New Mention';
const body = fromUser.name + ' ' + notification.message;

const toUsers = await this.userRepository.find({
where: {
or: mentions
Expand Down Expand Up @@ -638,7 +686,12 @@ export class NotificationService {
);

if (found) {
return this.fcmService.sendNotification(toUser.fcmTokens, found);
return this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
found,
);
}

return;
Expand Down Expand Up @@ -683,8 +736,13 @@ export class NotificationService {
);
if (createdNotification == null) return false;

const title = 'Send Tips Success';
const body = fromUser.name + ' ' + notification.message;

await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);

Expand All @@ -707,8 +765,13 @@ export class NotificationService {
);
if (createdNotification == null) return false;

const title = 'Send Reward Success';
const body = 'Myriad Official' + ' ' + notification.message;

await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);

Expand All @@ -731,8 +794,13 @@ export class NotificationService {
);
if (createdNotification == null) return false;

const title = 'Send Initial AUSD Success';
const body = 'Myriad Official' + ' ' + notification.message;

await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);

Expand Down Expand Up @@ -770,8 +838,13 @@ export class NotificationService {
);
if (createdNotification == null) return false;

const title = 'Send Claim Tips Success';
const body = 'You ' + notification.message;

await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);
return true;
Expand All @@ -795,8 +868,15 @@ export class NotificationService {

if (createdNotification === null) return false;

const title = `Connected ${
platform[0].toUpperCase() + platform.substring(1)
} Success`;
const body = 'You ' + notification.message;

await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);

Expand Down Expand Up @@ -825,8 +905,15 @@ export class NotificationService {

if (createdNotification === null) return false;

const title = `Disconnected ${
platform[0].toUpperCase() + platform.substring(1)
} Success`;
const body = 'You ' + notification.message;

await this.fcmService.sendNotification(
toUser.fcmTokens,
title,
body,
createdNotification,
);

Expand Down