React Native Firebase integration for Firebase Cloud Messaging (FCM) providing cross-platform push notification capabilities
—
Complete message lifecycle management for handling FCM messages in different app states: foreground, background, and when the app is launched from a notification.
Handles messages received while the app is active and in the foreground. These messages do not automatically display notifications.
/**
* Called when any FCM payload is received while app is in foreground
* @param listener - Callback function receiving the RemoteMessage
* @returns Function to unsubscribe from message events
*/
function onMessage(listener: (message: RemoteMessage) => any): () => void;Usage Examples:
import messaging from '@react-native-firebase/messaging';
// Listen for foreground messages
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log('FCM Message Data:', remoteMessage.data);
console.log('Notification payload:', remoteMessage.notification);
// Handle the message (show custom notification, update UI, etc.)
if (remoteMessage.notification) {
showCustomNotification(remoteMessage.notification);
}
});
// Unsubscribe when component unmounts
unsubscribe();
// Modular API
import { getMessaging, onMessage } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
const unsubscribe = onMessage(messagingInstance, remoteMessage => {
console.log('Message received:', remoteMessage);
});Sets a global handler for messages received when the app is in the background or terminated. Must be called outside of React components.
/**
* Set a message handler function called when app is in background or terminated
* @param handler - Async function to handle background messages
* @returns void
*/
function setBackgroundMessageHandler(handler: (message: RemoteMessage) => Promise<any>): void;Usage Examples:
// index.js or App.js (outside of React components)
import messaging from '@react-native-firebase/messaging';
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
// Perform background tasks
await updateLocalStorage(remoteMessage.data);
await sendAnalytics('background_message_received');
// Note: Avoid heavy processing that might timeout
});
// Modular API
import { getMessaging, setBackgroundMessageHandler } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
setBackgroundMessageHandler(messagingInstance, async remoteMessage => {
console.log('Background message:', remoteMessage);
});Handles when the user taps a notification to open the app from a background state.
/**
* Called when user presses a notification and app opens from background
* @param listener - Callback function receiving the RemoteMessage
* @returns Function to unsubscribe from notification opened events
*/
function onNotificationOpenedApp(listener: (message: RemoteMessage) => any): () => void;Usage Examples:
import messaging from '@react-native-firebase/messaging';
// Handle notification taps from background
const unsubscribe = messaging().onNotificationOpenedApp(remoteMessage => {
console.log('Notification caused app to open from background:', remoteMessage);
// Navigate to specific screen based on message data
if (remoteMessage.data?.screen) {
navigateToScreen(remoteMessage.data.screen);
}
});
// Modular API
import { getMessaging, onNotificationOpenedApp } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
const unsubscribe = onNotificationOpenedApp(messagingInstance, remoteMessage => {
console.log('App opened from notification:', remoteMessage);
});Retrieves the notification that caused the app to launch from a completely closed state.
/**
* Get notification that opened the app from a quit state
* @returns Promise resolving to RemoteMessage or null if app wasn't opened by notification
*/
function getInitialNotification(): Promise<RemoteMessage | null>;Usage Examples:
import messaging from '@react-native-firebase/messaging';
// Check if app was opened by a notification
const initialNotification = await messaging().getInitialNotification();
if (initialNotification) {
console.log('App opened by notification:', initialNotification);
// Handle the initial notification
if (initialNotification.data?.deepLink) {
navigateToDeepLink(initialNotification.data.deepLink);
}
} else {
console.log('App opened normally');
}
// Modular API
import { getMessaging, getInitialNotification } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
const initialNotification = await getInitialNotification(messagingInstance);Checks if the app is running in headless mode (launched in background by a data-only message).
/**
* Returns whether root view is headless
* @returns Promise resolving to boolean indicating headless state
*/
function getIsHeadless(): Promise<boolean>;Usage Examples:
import messaging from '@react-native-firebase/messaging';
// Check if app is running headless
const isHeadless = await messaging().getIsHeadless();
if (isHeadless) {
console.log('App launched in background by FCM');
// Avoid UI operations in headless mode
} else {
console.log('App has active UI');
// Safe to perform UI operations
}
// Modular API
import { getMessaging, getIsHeadless } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
const isHeadless = await getIsHeadless(messagingInstance);onMessage listener receives messagessetBackgroundMessageHandler processes messagesgetInitialNotification can retrieve launch message// Complete message handling setup
import messaging from '@react-native-firebase/messaging';
// Background handler (called outside components)
messaging().setBackgroundMessageHandler(async remoteMessage => {
await processBackgroundMessage(remoteMessage);
});
// In your main component
useEffect(() => {
// Foreground messages
const unsubscribeForeground = messaging().onMessage(async remoteMessage => {
showInAppNotification(remoteMessage);
});
// Background to foreground
const unsubscribeOpened = messaging().onNotificationOpenedApp(remoteMessage => {
handleNotificationNavigation(remoteMessage);
});
// Check if launched by notification
messaging().getInitialNotification().then(remoteMessage => {
if (remoteMessage) {
handleNotificationNavigation(remoteMessage);
}
});
return () => {
unsubscribeForeground();
unsubscribeOpened();
};
}, []);Install with Tessl CLI
npx tessl i tessl/npm-react-native-firebase--messaging