← Back to portfolio

Adding Firebase Push Notifications in React Native

July 1, 20268 min read
React NativeFirebaseNotificationsMobile

Push notifications are vital for user engagement. In modern React Native apps, Firebase Cloud Messaging (FCM) is the industry standard. This guide shows how to request user permissions, retrieve FCM registration tokens, configure system permissions, and handle notifications in both foreground and background application states.

1. Native Setup (AndroidManifest.xml & Xcode Capabilities)

On Android, to support push notifications on Android 13+ (API 33), you must declare the POST_NOTIFICATIONS permission in your manifest file:

xml
<!-- android/app/src/main/AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    
    <application ...>
      <!-- Add notification channel metadata if needed -->
    </application>
</manifest>

On iOS, open Xcode and select your project's target. Under the 'Signing & Capabilities' tab, add 'Push Notifications' and 'Background Modes'. Within 'Background Modes', make sure 'Remote notifications' is enabled.

2. Requesting User Permissions & Fetching Token

Install `@react-native-firebase/app` and `@react-native-firebase/messaging`. To receive remote notifications, you must request active user consent, particularly on iOS devices:

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

export async function requestUserPermission() {
  const authStatus = await messaging().requestPermission();
  const enabled =
    authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
    authStatus === messaging.AuthorizationStatus.PROVISIONAL;

  if (enabled) {
    console.log('Authorization status:', authStatus);
    const token = await messaging().getToken();
    console.log('FCM Token:', token);
    // Send token to your backend database mapping
  }
}

3. Registering React Native Message Listeners

Set up listeners in your application's entrypoint (e.g., App.tsx) to handle remote payloads when the app is foregrounded, backgrounded, or launched from a cold start:

typescript
import React, { useEffect } from 'react';
import { Alert } from 'react-native';
import messaging from '@react-native-firebase/messaging';

export default function App() {
  useEffect(() => {
    // 1. Listen for foreground notifications
    const unsubscribe = messaging().onMessage(async remoteMessage => {
      Alert.alert(
        remoteMessage.notification?.title || 'Notification',
        remoteMessage.notification?.body || ''
      );
    });

    // 2. Listen when app is opened from a background state
    messaging().onNotificationOpenedApp(remoteMessage => {
      console.log('Notification caused app to open from background state:', remoteMessage);
    });

    // 3. Check if app was opened from a closed state by notification
    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        if (remoteMessage) {
          console.log('Notification caused app to open from quit state:', remoteMessage);
        }
      });

    return unsubscribe;
  }, []);

  return null;
}