React Native Firebase integration for Firebase Cloud Messaging (FCM) providing cross-platform push notification capabilities
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
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.
npm install @react-native-firebase/messaging@react-native-firebase/app as peer dependencyESM/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;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);
});
}React Native Firebase Messaging is built around several key components:
firebase.messaging()) and modular (getMessaging()) APIsCore 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;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;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>;Subscribe and unsubscribe devices to/from topics for targeted messaging campaigns.
function subscribeToTopic(topic: string): Promise<void>;
function unsubscribeFromTopic(topic: string): Promise<void>;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;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;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 onlyinterface 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,
}