Skip to content

Commit

Permalink
fix(providers): Patch FCM API
Browse files Browse the repository at this point in the history
Use sendEachForMulticast instead of sendMulticast that is now deprecated.

See firebase/firebase-admin-node#2687
  • Loading branch information
SokratisVidros committed Sep 10, 2024
1 parent 64a7fe6 commit e52cbbe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.getMessaging = () => ({
sendToDevice: jest.fn(),
sendMulticast: jest.fn(),
sendEachForMulticast: jest.fn(),
});
2 changes: 1 addition & 1 deletion providers/fcm/src/lib/fcm.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ beforeEach(() => {
spy = jest
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
.spyOn(provider.messaging, 'sendMulticast')
.spyOn(provider.messaging, 'sendEachForMulticast')
.mockImplementation(async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return {} as any;
Expand Down
14 changes: 7 additions & 7 deletions providers/fcm/src/lib/fcm.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class FcmPushProvider implements IPushProvider {
projectId: string;
email: string;
secretKey: string;
}
},
) {
this.config = config;
this.appName = crypto.randomBytes(32).toString();
Expand All @@ -38,13 +38,13 @@ export class FcmPushProvider implements IPushProvider {
privateKey: this.config.secretKey,
}),
},
this.appName
this.appName,
);
this.messaging = getMessaging(firebase);
}

async sendMessage(
options: IPushOptions
options: IPushOptions,
): Promise<ISendMessageSuccessResponse> {
const {
deviceTokens: _,
Expand All @@ -65,7 +65,7 @@ export class FcmPushProvider implements IPushProvider {
let res;

if (type === 'data') {
res = await this.messaging.sendMulticast({
res = await this.messaging.sendEachForMulticast({
tokens: options.target,
data: {
...payload,
Expand All @@ -79,7 +79,7 @@ export class FcmPushProvider implements IPushProvider {
webpush,
});
} else {
res = await this.messaging.sendMulticast({
res = await this.messaging.sendEachForMulticast({
tokens: options.target,
notification: {
title: options.title,
Expand All @@ -98,7 +98,7 @@ export class FcmPushProvider implements IPushProvider {
throw new Error(
`Sending message failed due to "${
res.responses.find((i) => i.success === false).error.message
}"`
}"`,
);
}

Expand All @@ -109,7 +109,7 @@ export class FcmPushProvider implements IPushProvider {
ids: res?.responses?.map((response, index) =>
response.success
? response.messageId
: `${response.error.message}. Invalid token:- ${options.target[index]}`
: `${response.error.message}. Invalid token:- ${options.target[index]}`,
),
date: new Date().toISOString(),
};
Expand Down

0 comments on commit e52cbbe

Please sign in to comment.