CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-native-firebase--messaging

React Native Firebase integration for Firebase Cloud Messaging (FCM) providing cross-platform push notification capabilities

Pending
Overview
Eval results
Files

React Native Firebase Messaging

React Native Firebase Messaging provides comprehensive Firebase Cloud Messaging (FCM) integration for React Native applications. It enables cross-platform push notifications with native performance, supporting both Android and iOS platforms with advanced features like background message handling, notification customization, and topic subscriptions.

Package Information

  • Package Name: @react-native-firebase/messaging
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @react-native-firebase/messaging
  • Dependencies: Requires @react-native-firebase/app as peer dependency

Core Imports

ESM/TypeScript:

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

Modular API:

import { 
  getMessaging, 
  getToken, 
  onMessage, 
  requestPermission 
} from '@react-native-firebase/messaging';

CommonJS:

const messaging = require('@react-native-firebase/messaging').default;

Basic Usage

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

// Initialize and request permissions (iOS)
const authStatus = await messaging().requestPermission();
if (authStatus === messaging.AuthorizationStatus.AUTHORIZED) {
  // Get FCM registration token
  const token = await messaging().getToken();
  console.log('FCM Token:', token);
  
  // Listen for foreground messages
  const unsubscribe = messaging().onMessage(async remoteMessage => {
    console.log('Message:', remoteMessage.data);
  });
  
  // Setup background message handler
  messaging().setBackgroundMessageHandler(async remoteMessage => {
    console.log('Background message:', remoteMessage);
  });
}

Architecture

React Native Firebase Messaging is built around several key components:

  • Dual API Patterns: Both namespaced (firebase.messaging()) and modular (getMessaging()) APIs
  • Native Bridge: Communicates with native Firebase SDKs on Android and iOS
  • Message Lifecycle: Handles messages in foreground, background, and quit states
  • Token Management: Automatic token generation and refresh handling
  • Platform Abstraction: Unified API across platforms with platform-specific features
  • Background Processing: Headless task execution for background message handling

Capabilities

Token Management

Core FCM token operations for device registration and authentication with Firebase servers.

function getToken(options?: GetTokenOptions & NativeTokenOptions): Promise<string>;
function deleteToken(options?: NativeTokenOptions): Promise<void>;
function onTokenRefresh(listener: (token: string) => any): () => void;

Token Management

Message Handling

Complete message lifecycle management for foreground, background, and app launch scenarios.

function onMessage(listener: (message: RemoteMessage) => any): () => void;
function onNotificationOpenedApp(listener: (message: RemoteMessage) => any): () => void;
function getInitialNotification(): Promise<RemoteMessage | null>;
function setBackgroundMessageHandler(handler: (message: RemoteMessage) => Promise<any>): void;

Message Handling

Permissions & Registration

Permission management and device registration for receiving notifications, primarily for iOS.

function requestPermission(permissions?: IOSPermissions): Promise<AuthorizationStatus>;
function hasPermission(): Promise<AuthorizationStatus>;
function registerDeviceForRemoteMessages(): Promise<void>;
function unregisterDeviceForRemoteMessages(): Promise<void>;

Permissions & Registration

Topic Management

Subscribe and unsubscribe devices to/from topics for targeted messaging campaigns.

function subscribeToTopic(topic: string): Promise<void>;
function unsubscribeFromTopic(topic: string): Promise<void>;

Topic Management

iOS Specific Features

Apple Push Notification service (APNs) integration and iOS-specific messaging features.

function getAPNSToken(): Promise<string | null>;
function setAPNSToken(token: string, type?: string): Promise<void>;
function getDidOpenSettingsForNotification(): Promise<boolean>;
function setOpenSettingsForNotificationsHandler(handler: (message: RemoteMessage) => any): void;

iOS Features

Android Specific Features

Android-specific messaging capabilities including upstream messaging and message lifecycle events.

function sendMessage(message: RemoteMessage): Promise<void>;
function onDeletedMessages(listener: () => void): () => void;
function onMessageSent(listener: (messageId: string) => any): () => void;
function onSendError(listener: (evt: SendErrorEvent) => any): () => void;

Android Features

Configuration & Analytics

Configuration management and analytics settings for Firebase Cloud Messaging.

readonly isAutoInitEnabled: boolean;
function setAutoInitEnabled(enabled: boolean): Promise<void>;
readonly isDeliveryMetricsExportToBigQueryEnabled: boolean;
function setDeliveryMetricsExportToBigQuery(enabled: boolean): Promise<void>;
readonly isNotificationDelegationEnabled: boolean;
function setNotificationDelegationEnabled(enabled: boolean): Promise<void>;
function getIsHeadless(): Promise<boolean>;
function isSupported(): Promise<boolean>;
function experimentalSetDeliveryMetricsExportedToBigQueryEnabled(enabled: boolean): Promise<void>; // Web only

Global Types

interface RemoteMessage {
  messageId?: string;
  messageType?: string;
  from?: string;
  to?: string;
  collapseKey?: string;
  sentTime?: number;
  ttl?: number;
  data?: { [key: string]: string | object };
  notification?: Notification;
  contentAvailable?: boolean; // iOS
  mutableContent?: boolean; // iOS
  category?: string; // iOS
  threadId?: string; // iOS
  fcmOptions?: FcmOptions;
  priority?: MessagePriority; // Android
  originalPriority?: MessagePriority; // Android
}

interface Notification {
  title?: string;
  body?: string;
  titleLocKey?: string;
  titleLocArgs?: string[];
  bodyLocKey?: string;
  bodyLocArgs?: string[];
  icon?: string; // Web
  image?: string; // Web
  ios?: {
    subtitle?: string;
    subtitleLocKey?: string;
    subtitleLocArgs?: string[];
    badge?: string;
    sound?: string | NotificationIOSCriticalSound;
  };
  android?: {
    sound?: string;
    channelId?: string;
    color?: string;
    smallIcon?: string;
    imageUrl?: string;
    link?: string;
    count?: number;
    clickAction?: string;
    priority?: NotificationAndroidPriority;
    ticker?: string;
    visibility?: NotificationAndroidVisibility;
  };
}

interface NotificationIOSCriticalSound {
  critical?: boolean;
  name: string;
  volume?: number;
}

interface FcmOptions {
  link?: string;
  analyticsLabel?: string;
}

interface IOSPermissions {
  alert?: boolean;
  announcement?: boolean;
  badge?: boolean;
  criticalAlert?: boolean;
  carPlay?: boolean;
  provisional?: boolean;
  sound?: boolean;
  providesAppNotificationSettings?: boolean;
}

interface GetTokenOptions {
  vapidKey?: string; // Web
  serviceWorkerRegistration?: ServiceWorkerRegistration; // Web
}

interface NativeTokenOptions {
  appName?: string; // Android
  senderId?: string; // iOS
}

interface SendErrorEvent {
  messageId: string;
  error: NativeFirebaseError;
}

enum AuthorizationStatus {
  NOT_DETERMINED = -1, // iOS
  DENIED = 0,
  AUTHORIZED = 1,
  PROVISIONAL = 2, // iOS 12+
  EPHEMERAL = 3, // iOS 14+
}

enum MessagePriority {
  PRIORITY_UNKNOWN = 0,
  PRIORITY_HIGH = 1,
  PRIORITY_NORMAL = 2,
}

enum NotificationAndroidPriority {
  PRIORITY_MIN = -2,
  PRIORITY_LOW = -1,
  PRIORITY_DEFAULT = 0,
  PRIORITY_HIGH = 1,
  PRIORITY_MAX = 2,
}

enum NotificationAndroidVisibility {
  VISIBILITY_SECRET = -1,
  VISIBILITY_PRIVATE = 0,
  VISIBILITY_PUBLIC = 1,
}

Install with Tessl CLI

npx tessl i tessl/npm-react-native-firebase--messaging
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@react-native-firebase/messaging@23.3.x
Publish Source
CLI
Badge
tessl/npm-react-native-firebase--messaging badge