Teardown

FirebaseMessagingAdapter

Notification adapter for Firebase Cloud Messaging.

Work in Progress: The notification adapter APIs are under active development and may change in future releases.

The FirebaseMessagingAdapter integrates with Firebase Cloud Messaging for push notifications.

Installation

npm install @react-native-firebase/app @react-native-firebase/messaging
# or
bun add @react-native-firebase/app @react-native-firebase/messaging

For iOS:

cd ios && pod install

Usage

import { TeardownCore } from '@teardown/force-updates';
import { FirebaseMessagingAdapter } from '@teardown/force-updates/firebase';
import { DeviceInfoAdapter } from '@teardown/force-updates/adapters/device-info';
import { AsyncStorageAdapter } from '@teardown/force-updates/adapters/async-storage';

const teardown = new TeardownCore({
  org_id: 'your-org-id',
  project_id: 'your-project-id',
  api_key: 'your-api-key',
  storageAdapter: new AsyncStorageAdapter(),
  deviceAdapter: new DeviceInfoAdapter(),
  notificationAdapter: new FirebaseMessagingAdapter(),
});

// Access via NotificationsClient
if (teardown.notifications) {
  const status = await teardown.notifications.requestPermissions();
  if (status.granted) {
    const token = await teardown.notifications.getToken();
    console.log('FCM token:', token);
  }
}

Import Path

import { FirebaseMessagingAdapter } from '@teardown/force-updates/firebase';

API

All methods are accessed via NotificationsClient, not directly on the adapter.

requestPermissions()

Request notification permissions (required on iOS):

const status = await teardown.notifications.requestPermissions();
// Returns: { granted: boolean, canAskAgain: boolean }

getToken()

Get the FCM registration token:

const token = await teardown.notifications.getToken();
// Returns: FCM token string or null

onNotificationReceived()

Subscribe to foreground notifications:

const unsubscribe = teardown.notifications.onNotificationReceived((notification) => {
  console.log('Received:', notification.title);
});

// Cleanup
unsubscribe();

onNotificationOpened()

Subscribe to notification taps:

const unsubscribe = teardown.notifications.onNotificationOpened((notification) => {
  console.log('User tapped:', notification.title);
});

// Cleanup
unsubscribe();

onTokenRefresh()

Subscribe to token changes:

const unsubscribe = teardown.notifications.onTokenChange((token) => {
  console.log('New FCM token:', token);
});

// Cleanup
unsubscribe();

onDataMessage()

Subscribe to data-only messages (silent push):

const unsubscribe = teardown.notifications.onDataMessage((message) => {
  console.log('Data message:', message.data);
});

// Cleanup
unsubscribe();

Configuration

Android

  1. Add google-services.json to android/app/
  2. Add to android/build.gradle:
buildscript {
  dependencies {
    classpath 'com.google.gms:google-services:4.3.15'
  }
}
  1. Add to android/app/build.gradle:
apply plugin: 'com.google.gms.google-services'

iOS

  1. Add GoogleService-Info.plist to iOS project
  2. Enable Push Notifications capability in Xcode
  3. Upload APNs key to Firebase Console

Background Messages

Handle messages when app is in background:

import messaging from '@react-native-firebase/messaging';

// Register background handler (must be outside component)
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
  console.log('Background message:', remoteMessage);
});

Platform

Returns NotificationPlatformEnum.FCM

Requirements

  • @react-native-firebase/app >= 18.0.0
  • @react-native-firebase/messaging >= 18.0.0
  • React Native >= 0.71