Skip to content

Commit

Permalink
refactor(push-notifications): send notification always fetch all user…
Browse files Browse the repository at this point in the history
… tokens for specified platform (#979)

* refactor(push-notifications): send notification always fetch all user tokens for specified platform
fix(push-notifications): firebase notifications crash due to expired tokens

* fix(push-notifications): missing platform param in sendToDevice
fix(push-notifications): platform not considered in send many messages
  • Loading branch information
kkopanidis committed Mar 7, 2024
1 parent 1e9db8f commit 8b0835a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import ConduitGrpcSdk from '@conduitplatform/grpc-sdk';
import { getMessaging, Message, Messaging } from 'firebase-admin/messaging';
import { cert, initializeApp, ServiceAccount } from 'firebase-admin/app';
import { NotificationToken } from '../models/index.js';

export class FirebaseProvider extends BaseNotificationProvider<IFirebaseSettings> {
private fcm?: Messaging;
Expand Down Expand Up @@ -76,6 +77,9 @@ export class FirebaseProvider extends BaseNotificationProvider<IFirebaseSettings
},
};
}
return this.fcm!.send(message);
return this.fcm!.send(message).catch(e => {
ConduitGrpcSdk.Logger.error('Failed to send notification: ', e);
NotificationToken.getInstance().deleteOne({ token });
});
}
}
19 changes: 12 additions & 7 deletions modules/push-notifications/src/providers/base.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,26 @@ export class BaseNotificationProvider<T> {
});
}
if (this.isBaseProvider) return;
const notificationToken = (await this.fetchTokens(sendTo)) as NotificationToken;
if (isNil(notificationToken)) {
const notificationTokens = await this.fetchTokens(sendTo, params.platform);
if (notificationTokens.length === 0) {
throw new Error('No notification token found');
}
return this.sendMessage(notificationToken.token, params);
const promises = notificationTokens.map(async notToken => {
await this.sendMessage(notToken.token, params);
});
return Promise.all(promises);
}

fetchTokens(
users: string | string[],
): Promise<NotificationToken | NotificationToken[] | null> {
fetchTokens(users: string | string[], platform?: string): Promise<NotificationToken[]> {
if (Array.isArray(users)) {
return NotificationToken.getInstance().findMany({
userId: { $in: users },
...(platform ? { platform } : {}),
});
}
return NotificationToken.getInstance().findOne({
return NotificationToken.getInstance().findMany({
userId: users as string,
...(platform ? { platform } : {}),
});
}

Expand Down Expand Up @@ -90,6 +93,7 @@ export class BaseNotificationProvider<T> {
const promises = notificationTokens.map(async token => {
const id = token.userId.toString();
const data = notificationsObj[id];
if (data.platform && data.platform !== token.platform) return;
await this.sendMessage(token.token, data).catch(e => {
ConduitGrpcSdk.Logger.error(e);
});
Expand All @@ -115,6 +119,7 @@ export class BaseNotificationProvider<T> {
if (this.isBaseProvider) return;
const notificationTokens = (await this.fetchTokens(
params.sendTo,
params.platform,
)) as NotificationToken[];

if (notificationTokens.length === 0) throw new Error('Could not find tokens');
Expand Down

0 comments on commit 8b0835a

Please sign in to comment.