AlgorithmX
Integrations4 min read

React Native Firebase

Register FCM tokens on iOS and Android, and forward Android messages through the optional AlgorithmX adapter.

Use this path when your app uses @react-native-firebase/messaging. Complete native installation and startup first and use AlgorithmX SDK 1.1.1 or later. The optional adapter uses the modular messaging API and accepts versions 22–26; types were checked with 22.4 and 26.4.

Keep the app's existing Firebase configuration. Add the matching google-services.json for Android and GoogleService-Info.plist for iOS, and complete the shared Firebase setup. Do not add an AlgorithmX native messaging service alongside Firebase's service.

This route fits an app where Firebase already delivers Android messages to JavaScript. The adapter sorts those messages: AlgorithmX handles its own campaigns, and your existing code continues handling the others. You will connect the foreground listener, then add a check to the background handler you already have.

1. Connect foreground messages and tokens

Call this once in your application startup file, outside screen components:

ts
import { connectFirebaseMessaging } from '@algorithmxcloud/react-native-sdk/firebase';

const connection = connectFirebaseMessaging({
  onOtherMessage: async (message) => {
    // Move your existing Android foreground processing here.
    await handleOtherForegroundMessage(message);
  },
  onError: (error) => reportPushError(error),
});

connection.ready.catch(reportPushError);

handleOtherForegroundMessage and reportPushError are your app's functions. Await connection.ready inside your existing async startup flow. The connection registers the current FCM token on Android and iOS, observes token refresh on both, and forwards AlgorithmX foreground messages on Android. Other Android messages go to onOtherMessage. Remove your old Android foreground subscription if its work has moved into this callback, so it does not also display AlgorithmX notifications.

Check: start each app and confirm its FCM token reaches AlgorithmX. Test one of your existing Android notifications too, so you know its processing still reaches onOtherMessage.

On iOS the adapter registers for remote messages if React Native Firebase reports that registration is disabled, then requests the FCM token. Apple registration/mapping must be ready. If connection.ready rejects, its listeners are removed: report the error and create a new connection after registration is ready. It does not request notification permission or install an iOS foreground-message listener. Keep your existing iOS Firebase listeners and the native AlgorithmX notification callbacks. Do not register the raw APNs token with AlgorithmX.

2. Add one check to your background handler

A background message can arrive when no React screen is mounted. That is why this handler belongs in the entry file, rather than inside a screen.

Keep the background handler in index.js, registered before the app component. If the app already has one, add this check at its top:

ts
import { getMessaging, setBackgroundMessageHandler } from '@react-native-firebase/messaging';
import { handleFirebaseMessage } from '@algorithmxcloud/react-native-sdk/firebase';

setBackgroundMessageHandler(getMessaging(), async (message) => {
  if (await handleFirebaseMessage(message)) return;
  await handleOtherBackgroundMessage(message); // Your existing processing.
});

The helper resolves true after Android's native SDK accepts an AlgorithmX message. It returns false for other messages, and on iOS, which uses native APNs callbacks. Do not forward iOS AlgorithmX messages a second time from your existing background code. Preserve the app's other iOS processing.

The SDK uses native payload ownership checks; you do not need to duplicate its action-name checks in your app. A handling error rejects the promise so your handler can report it.

Check: send an AlgorithmX notification with the Android app in the background. It should appear once. Then send an unrelated message and confirm your existing background handler processes it.

Permissions and cleanup

The adapter never asks for notification permission. Keep your existing prompt and Android notification-channel setup. On an active connection whose startup succeeded, await connection.refreshToken() registers the current FCM token again on either platform, for example after changing permissions.

Keep the connection alive for the application lifetime. connection.remove() stops its foreground and token subscriptions when you explicitly tear it down; it does not remove your background handler.

Use iOS push setup on Apple devices, then customers, events, and navigation. Test both AlgorithmX and existing notifications with the release checklist.