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

token-management.mddocs/

Token Management

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.

Capabilities

Get FCM Token

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);

Delete FCM Token

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);

Token Refresh Listener

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);
});

Configuration Properties

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);

Token Lifecycle

  1. Initial Token: Generated when app first requests messaging permissions
  2. Token Refresh: Automatic refresh when tokens expire or app is restored
  3. Token Deletion: Manual deletion for logout or app uninstall scenarios
  4. Server Sync: Tokens should be synchronized with your backend server

Error Handling

Common token-related errors:

  • Unregistered: Device not registered for remote notifications (iOS)
  • Network Error: Unable to connect to Firebase servers
  • Invalid Configuration: Missing or incorrect Firebase configuration
  • Permission Denied: User has denied notification permissions

Install with Tessl CLI

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

docs

android-features.md

index.md

ios-features.md

message-handling.md

permissions-registration.md

token-management.md

topic-management.md

tile.json