From c8b7909cad83ebf33c7c481c9e4ee62058eab757 Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Fri, 15 Nov 2024 19:18:42 -0500 Subject: [PATCH] test(messaging): sendFCM function that reflects a TokenMessage - requires a TokenMessage, and just sends the Message to the device addressed by the token in the message - optionally can have a delay so you can put a test app in background or kill it or similar after calling the API For security purposes: - Tested this with an incorrect google-services.json / plist and the function call simply fails - Tested this with an incorrect package name and the function all also simply fails - Tested it with a correct package name and you can send on android, but on iOS there is still a lot of Apple developer account / app work to do, so that fails. --- .../workflows/scripts/functions/src/index.ts | 1 + .../scripts/functions/src/sendFCM.ts | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 .github/workflows/scripts/functions/src/sendFCM.ts diff --git a/.github/workflows/scripts/functions/src/index.ts b/.github/workflows/scripts/functions/src/index.ts index dbfae53b79..9d15cc2e2c 100644 --- a/.github/workflows/scripts/functions/src/index.ts +++ b/.github/workflows/scripts/functions/src/index.ts @@ -23,3 +23,4 @@ export { testFunctionCustomRegion } from './testFunctionCustomRegion'; export { testFunctionDefaultRegionV2 } from './testFunctionDefaultRegion'; export { testFunctionRemoteConfigUpdateV2 } from './testFunctionRemoteConfigUpdate'; export { fetchAppCheckTokenV2 } from './fetchAppCheckToken'; +export { sendFCM } from './sendFCM'; diff --git a/.github/workflows/scripts/functions/src/sendFCM.ts b/.github/workflows/scripts/functions/src/sendFCM.ts new file mode 100644 index 0000000000..2715491147 --- /dev/null +++ b/.github/workflows/scripts/functions/src/sendFCM.ts @@ -0,0 +1,28 @@ +/* + * + * Testing tools for invertase/react-native-firebase use only. + * + * Copyright (C) 2018-present Invertase Limited + * + * See License file for more information. + */ + +import * as functions from 'firebase-functions/v2'; +import { CallableRequest } from 'firebase-functions/v2/https'; + +import { getMessaging, TokenMessage } from 'firebase-admin/messaging'; + +// Note: this will only work in a live environment, not locally via the Firebase emulator. +export const sendFCM = functions.https.onCall( + async (req: CallableRequest<{ message: TokenMessage; delay?: number }>) => { + const { message, delay } = req.data; + return await new Promise(() => { + functions.logger.info('Sleeping this many milliseconds: ' + (delay ?? 0)); + setTimeout(async () => { + functions.logger.info('done sleeping'); + const result = await getMessaging().send(message); + return { messageId: result }; + }, delay ?? 0); + }); + }, +);