Push Notifications
Push notifications in React Native involve communication between an app, a backend server, and a push notification service (e.g., Apple Push Notification Service (APNs) for iOS or Firebase Cloud Messaging (FCM) for Android).
User Flow Overview
-
User Opt-In:
- The app asks for the user’s permission to send push notifications.
- On iOS, this is required by the operating system.
- On Android, permissions are usually granted during app installation.
-
Device Registration:
- The app registers itself with a push notification service (APNs for iOS, FCM for Android).
- The service returns a Device Token (iOS) or a Registration Token (Android), which uniquely identifies the app instance on the device.
- Server Configuration:
- The backend server securely stores the device tokens and is configured to communicate with the respective push notification services.
- Push Notification Sending:
- The backend server sends a notification request (with a payload and token) to APNs/FCM.
- The service routes the notification to the target device using the token.
- Notification Received:
- The app receives the notification via an event listener or displays it in the notification center.
How to set up notifications
-
Choose a Library
react-native-push-notification(not actively maintained)@react-native-firebase/messagingnotifee/react-native-notifee(great for rich notification control, advanced local notification handling, works with Firebase)
For most modern apps, React Native Firebase Messaging + Notifee is the go-to combo.
-
Set up Firebase
-
Android:
- Add your Android app in Firebase Console
- Download
google-services.jsonand put it inandroid/app/ - Update your
android/build.gradleandandroid/app/build.gradlefor Firebase and messaging
-
iOS:
-
Add your iOS app in Firebase Console
-
Download
GoogleService-Info.plistand add it using Xcode -
Go to project target, then Signing & Capabilities and Enable Notifications & Background Modes -> 'Remote Notifications' in the Xcode project settings
-
Setup APNs and upload the key to Firebase
-
In the app: request user permissions for iOS 10+ on app start:
import messaging from '@react-native-firebase/messaging';
const async requestUserPermission = () => {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Notification permission granted');
}
}
-
-
-
Get the FCM token
import messaging from '@react-native-firebase/messaging';
async function getFCMToken() {
const token = await messaging().getToken();
console.log('FCM Token:', token);
} -
Listen for messages
Foreground
useEffect(() => {
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
// Display a local notification (optional)
console.log('Foreground message:', remoteMessage);
});
return unsubscribe;
}, []);Background and Quit state
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('Background message:', remoteMessage);
});Local notifications from foreground messages
import notifee from '@notifee/react-native';
await notifee.displayNotification({
title: 'New Message',
body: 'You have a new notification',
});
APNs Certificate
A certificate generated by Apple and uploaded to your backend server.
How to Generate
- Log in to the Apple Developer Console.
- Create an App ID and enable the “Push Notifications” service.
- Generate a Certificate Signing Request (CSR) from your local machine.
- Upload the CSR to Apple to generate the APNs certificate.
- Download the certificate, convert it to .p12 or .pem format, and upload it to your backend server or push notification provider (e.g., Firebase).
Device Token
The device token is a unique identifier for the app instance. It's assigned to a specific installation of the app on a specific device.
On Android, it’s generated by FCM. On iOS, it comes from APNs and is passed through Firebase Messaging if you use it.
Simple flow
- User installs the app
- App requests permission to send push notifications
- Your app requests a device token from the notification service (APNs for iOS, FCM for Android).
- APNs/Firebase returns device token
- Device token is sent to backend server
- Backend server stores device token
- Backend server sends notification to device using device token
Getting the FCM token
const fcmToken = await messaging().getToken();
// fcmToken: 'dXzlLpz...abc123' ← This is your device token
The BE sends the notification like that:
{
"to": "dXzlLpz...abc123",
"notification": {
"title": "Hi!",
"body": "You have a new message"
}
}
If you don’t use Firebase and want the raw APNs token directly, you’d need to write native code or use a library like react-native-push-notification, but Firebase Messaging handles it internally and gives you an FCM token that works cross-platform.
Tokens can change over time — you must handle token refresh:
useEffect(() => {
const unsubscribe = messaging().onTokenRefresh((newToken) => {
console.log('New FCM Token:', newToken);
// Update token in backend
});
return unsubscribe;
}, []);