Skip to content

Commit

Permalink
test(messaging): sendFCM function that reflects a TokenMessage
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
mikehardy committed Nov 16, 2024
1 parent f32414d commit c8b7909
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/scripts/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export { testFunctionCustomRegion } from './testFunctionCustomRegion';
export { testFunctionDefaultRegionV2 } from './testFunctionDefaultRegion';
export { testFunctionRemoteConfigUpdateV2 } from './testFunctionRemoteConfigUpdate';
export { fetchAppCheckTokenV2 } from './fetchAppCheckToken';
export { sendFCM } from './sendFCM';
28 changes: 28 additions & 0 deletions .github/workflows/scripts/functions/src/sendFCM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* Testing tools for invertase/react-native-firebase use only.
*
* Copyright (C) 2018-present Invertase Limited <[email protected]>
*
* 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);
});
},
);

0 comments on commit c8b7909

Please sign in to comment.