diff --git a/packages/notification-services-controller/src/NotificationServicesPushController/NotificationServicesPushController.test.ts b/packages/notification-services-controller/src/NotificationServicesPushController/NotificationServicesPushController.test.ts index 2ae53a2be8..ef1505ca39 100644 --- a/packages/notification-services-controller/src/NotificationServicesPushController/NotificationServicesPushController.test.ts +++ b/packages/notification-services-controller/src/NotificationServicesPushController/NotificationServicesPushController.test.ts @@ -12,13 +12,14 @@ import type { PushNotificationEnv } from './types'; const MOCK_JWT = 'mockJwt'; const MOCK_FCM_TOKEN = 'mockFcmToken'; +const MOCK_MOBILE_FCM_TOKEN = 'mockMobileFcmToken'; const MOCK_TRIGGERS = ['uuid1', 'uuid2']; describe('NotificationServicesPushController', () => { - const arrangeServicesMocks = () => { + const arrangeServicesMocks = (token?: string) => { const activatePushNotificationsMock = jest .spyOn(services, 'activatePushNotifications') - .mockResolvedValue(MOCK_FCM_TOKEN); + .mockResolvedValue(token ?? MOCK_FCM_TOKEN); const deactivatePushNotificationsMock = jest .spyOn(services, 'deactivatePushNotifications') @@ -58,6 +59,20 @@ describe('NotificationServicesPushController', () => { expect(services.listenToPushNotifications).toHaveBeenCalled(); }); + + it('should update the state with provided mobile fcmToken', async () => { + arrangeServicesMocks(MOCK_MOBILE_FCM_TOKEN); + const { controller, messenger } = arrangeMockMessenger(); + mockAuthBearerTokenCall(messenger); + + await controller.enablePushNotifications( + MOCK_TRIGGERS, + MOCK_MOBILE_FCM_TOKEN, + ); + expect(controller.state.fcmToken).toBe(MOCK_MOBILE_FCM_TOKEN); + + expect(services.listenToPushNotifications).toHaveBeenCalled(); + }); }); describe('disablePushNotifications', () => { diff --git a/packages/notification-services-controller/src/NotificationServicesPushController/NotificationServicesPushController.ts b/packages/notification-services-controller/src/NotificationServicesPushController/NotificationServicesPushController.ts index 251f4a4a29..94d39a4210 100644 --- a/packages/notification-services-controller/src/NotificationServicesPushController/NotificationServicesPushController.ts +++ b/packages/notification-services-controller/src/NotificationServicesPushController/NotificationServicesPushController.ts @@ -248,8 +248,9 @@ export default class NotificationServicesPushController extends BaseController< * 3. Sending the FCM token to the server responsible for sending notifications, to register the device. * * @param UUIDs - An array of UUIDs to enable push notifications for. + * @param fcmToken - The optional FCM token to use for push notifications. */ - async enablePushNotifications(UUIDs: string[]) { + async enablePushNotifications(UUIDs: string[], fcmToken?: string) { if (!this.#config.isPushEnabled) { return; } @@ -267,6 +268,7 @@ export default class NotificationServicesPushController extends BaseController< bearerToken, triggers: UUIDs, env: this.#env, + fcmToken, createRegToken, platform: this.#config.platform, }).catch(() => null); diff --git a/packages/notification-services-controller/src/NotificationServicesPushController/services/services.test.ts b/packages/notification-services-controller/src/NotificationServicesPushController/services/services.test.ts index 830d3ed629..694d907f23 100644 --- a/packages/notification-services-controller/src/NotificationServicesPushController/services/services.test.ts +++ b/packages/notification-services-controller/src/NotificationServicesPushController/services/services.test.ts @@ -15,6 +15,7 @@ import { const MOCK_REG_TOKEN = 'REG_TOKEN'; const MOCK_NEW_REG_TOKEN = 'NEW_REG_TOKEN'; +const MOCK_MOBILE_FCM_TOKEN = 'mockMobileFcmToken'; const MOCK_TRIGGERS = ['1', '2', '3']; const MOCK_JWT = 'MOCK_JWT'; @@ -78,8 +79,15 @@ describe('NotificationServicesPushController Services', () => { env: {} as PushNotificationEnv, }; + const mobileParams = { + ...params, + fcmToken: MOCK_MOBILE_FCM_TOKEN, + platform: 'mobile' as const, + }; + return { params, + mobileParams, apis: { mockGet: mockEndpointGetPushNotificationLinks(override?.mockGet), mockPut: mockEndpointUpdatePushNotificationLinks(override?.mockPut), @@ -98,6 +106,17 @@ describe('NotificationServicesPushController Services', () => { expect(result).toBe(MOCK_NEW_REG_TOKEN); }); + it('should successfully call APIs and add provided mobile fcmToken', async () => { + const { mobileParams, apis } = arrangeMocks(); + const result = await activatePushNotifications(mobileParams); + + expect(apis.mockGet.isDone()).toBe(true); + expect(mobileParams.createRegToken).not.toHaveBeenCalled(); + expect(apis.mockPut.isDone()).toBe(true); + + expect(result).toBe(MOCK_MOBILE_FCM_TOKEN); + }); + it('should return null if unable to get links from API', async () => { const { params, apis } = arrangeMocks({ mockGet: { status: 500 } }); const result = await activatePushNotifications(params); diff --git a/packages/notification-services-controller/src/NotificationServicesPushController/services/services.ts b/packages/notification-services-controller/src/NotificationServicesPushController/services/services.ts index c77b27b863..1bb84b14a5 100644 --- a/packages/notification-services-controller/src/NotificationServicesPushController/services/services.ts +++ b/packages/notification-services-controller/src/NotificationServicesPushController/services/services.ts @@ -91,6 +91,7 @@ type ActivatePushNotificationsParams = { env: PushNotificationEnv; createRegToken: CreateRegToken; platform: 'extension' | 'mobile' | 'portfolio'; + fcmToken?: string; }; /** @@ -102,7 +103,8 @@ type ActivatePushNotificationsParams = { export async function activatePushNotifications( params: ActivatePushNotificationsParams, ): Promise { - const { bearerToken, triggers, env, createRegToken, platform } = params; + const { bearerToken, triggers, env, createRegToken, platform, fcmToken } = + params; const notificationLinks = await getPushNotificationLinks(bearerToken); @@ -110,7 +112,7 @@ export async function activatePushNotifications( return null; } - const regToken = await createRegToken(env).catch(() => null); + const regToken = fcmToken ?? (await createRegToken(env).catch(() => null)); if (!regToken) { return null; }