React Native Firebase integration for Firebase Cloud Messaging (FCM) providing cross-platform push notification capabilities
—
FCM token management for device registration and authentication with Firebase Cloud Messaging servers. Tokens are unique identifiers for each app installation and are required for sending targeted messages.
Retrieves the current FCM registration token for the device. This token should be sent to your server for targeting push notifications.
/**
* Returns an FCM token for this device
* @param options - Optional configuration for token retrieval
* @returns Promise resolving to the FCM token string
*/
function getToken(options?: GetTokenOptions & NativeTokenOptions): Promise<string>;
interface GetTokenOptions {
/** VAPID key for web push (web only) */
vapidKey?: string;
/** Service worker registration for web push (web only) */
serviceWorkerRegistration?: ServiceWorkerRegistration;
}
interface NativeTokenOptions {
/** Firebase app name (Android only) */
appName?: string;
/** Sender ID for push notifications (iOS only) */
senderId?: string;
}Usage Examples:
import messaging from '@react-native-firebase/messaging';
// Get default token
const token = await messaging().getToken();
console.log('FCM Token:', token);
// Get token with custom sender ID (iOS)
const tokenWithSender = await messaging().getToken({
senderId: 'your-sender-id'
});
// Modular API
import { getMessaging, getToken } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
const token = await getToken(messagingInstance);Removes access to an FCM token, causing messages sent to this token to fail. Useful for logout scenarios or when uninstalling push notifications.
/**
* Removes access to an FCM token previously authorized
* @param options - Optional configuration for token deletion
* @returns Promise that resolves when token is deleted
*/
function deleteToken(options?: NativeTokenOptions): Promise<void>;Usage Examples:
import messaging from '@react-native-firebase/messaging';
// Delete default token
await messaging().deleteToken();
// Delete token with specific options
await messaging().deleteToken({
appName: 'secondary-app', // Android
senderId: 'your-sender-id' // iOS
});
// Modular API
import { getMessaging, deleteToken } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
await deleteToken(messagingInstance);Monitors token refresh events. Tokens can be refreshed by Firebase when they expire or when the app is restored on a new device.
/**
* Called when a new registration token is generated for the device
* @param listener - Callback function receiving the new token
* @returns Function to unsubscribe from token refresh events
*/
function onTokenRefresh(listener: (token: string) => any): () => void;Usage Examples:
import messaging from '@react-native-firebase/messaging';
// Listen for token refresh
const unsubscribe = messaging().onTokenRefresh(token => {
console.log('New token:', token);
// Send new token to your server
updateTokenOnServer(token);
});
// Stop listening
unsubscribe();
// Modular API
import { getMessaging, onTokenRefresh } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
const unsubscribe = onTokenRefresh(messagingInstance, token => {
console.log('Token refreshed:', token);
});Properties for controlling token behavior and checking current status.
/** Whether messaging auto initialization is enabled */
readonly isAutoInitEnabled: boolean;
/**
* Sets whether auto initialization for messaging is enabled or disabled
* @param enabled - Boolean to enable or disable auto initialization
* @returns Promise that resolves when setting is updated
*/
function setAutoInitEnabled(enabled: boolean): Promise<void>;Usage Examples:
import messaging from '@react-native-firebase/messaging';
// Check if auto init is enabled
const autoInitEnabled = messaging().isAutoInitEnabled;
console.log('Auto init enabled:', autoInitEnabled);
// Disable auto initialization (useful for GDPR compliance)
await messaging().setAutoInitEnabled(false);
// Modular API
import { getMessaging, isAutoInitEnabled, setAutoInitEnabled } from '@react-native-firebase/messaging';
const messagingInstance = getMessaging();
const enabled = isAutoInitEnabled(messagingInstance);
await setAutoInitEnabled(messagingInstance, false);Common token-related errors:
Install with Tessl CLI
npx tessl i tessl/npm-react-native-firebase--messaging