From e52cbbe4bd702d487bf627fd2cfd8ee1ae53f301 Mon Sep 17 00:00:00 2001 From: Sokratis Vidros Date: Tue, 10 Sep 2024 16:39:32 +0200 Subject: [PATCH] fix(providers): Patch FCM API Use sendEachForMulticast instead of sendMulticast that is now deprecated. See https://github.com/firebase/firebase-admin-node/issues/2687 --- .../src/lib/__mocks__/firebase-admin/messaging.ts | 2 +- providers/fcm/src/lib/fcm.provider.spec.ts | 2 +- providers/fcm/src/lib/fcm.provider.ts | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/providers/fcm/src/lib/__mocks__/firebase-admin/messaging.ts b/providers/fcm/src/lib/__mocks__/firebase-admin/messaging.ts index 2a150442385..046cb76f2cb 100644 --- a/providers/fcm/src/lib/__mocks__/firebase-admin/messaging.ts +++ b/providers/fcm/src/lib/__mocks__/firebase-admin/messaging.ts @@ -1,4 +1,4 @@ exports.getMessaging = () => ({ sendToDevice: jest.fn(), - sendMulticast: jest.fn(), + sendEachForMulticast: jest.fn(), }); diff --git a/providers/fcm/src/lib/fcm.provider.spec.ts b/providers/fcm/src/lib/fcm.provider.spec.ts index 38214d4c638..fe74b2b6c80 100644 --- a/providers/fcm/src/lib/fcm.provider.spec.ts +++ b/providers/fcm/src/lib/fcm.provider.spec.ts @@ -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; diff --git a/providers/fcm/src/lib/fcm.provider.ts b/providers/fcm/src/lib/fcm.provider.ts index 65fc649d628..c120e2e7228 100644 --- a/providers/fcm/src/lib/fcm.provider.ts +++ b/providers/fcm/src/lib/fcm.provider.ts @@ -26,7 +26,7 @@ export class FcmPushProvider implements IPushProvider { projectId: string; email: string; secretKey: string; - } + }, ) { this.config = config; this.appName = crypto.randomBytes(32).toString(); @@ -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 { const { deviceTokens: _, @@ -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, @@ -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, @@ -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 - }"` + }"`, ); } @@ -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(), };