Push notifications let your app send messages to users even when they are not using it. FCM (Firebase Cloud Messaging) helps you send these messages easily and reliably.
Push notifications with FCM in React Native
import messaging from '@react-native-firebase/messaging'; // Request permission to receive notifications const requestUserPermission = async () => { const authStatus = await messaging().requestPermission(); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; return enabled; }; // Get FCM token const getFcmToken = async () => { const token = await messaging().getToken(); return token; }; // Listen for messages messaging().onMessage(async remoteMessage => { console.log('A new FCM message arrived!', remoteMessage); });
Use messaging().requestPermission() to ask user permission for notifications.
Use messaging().getToken() to get the device token needed to send notifications.
const requestPermission = async () => { const authStatus = await messaging().requestPermission(); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; if (enabled) { console.log('Permission granted'); } else { console.log('Permission denied'); } };
messaging().onMessage(async remoteMessage => { console.log('New notification: ' + remoteMessage.notification?.title); });
This React Native app requests notification permission on start, listens for messages, and shows the FCM token when the button is pressed.
import React, {useEffect} from 'react'; import {View, Text, Button, Alert} from 'react-native'; import messaging from '@react-native-firebase/messaging'; export default function App() { const requestUserPermission = async () => { const authStatus = await messaging().requestPermission(); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; if (enabled) { console.log('Permission granted'); } else { console.log('Permission denied'); } }; const getToken = async () => { const token = await messaging().getToken(); Alert.alert('FCM Token', token); }; useEffect(() => { requestUserPermission(); const unsubscribe = messaging().onMessage(async remoteMessage => { Alert.alert('FCM Message', remoteMessage.notification?.title || 'No title'); }); return unsubscribe; }, []); return ( <View style={{flex:1, justifyContent:'center', alignItems:'center'}}> <Text>Push Notifications with FCM</Text> <Button title="Get FCM Token" onPress={getToken} /> </View> ); }
Always test push notifications on a real device; emulators may not support them fully.
Remember to configure Firebase project and add google-services files to your app.
Handle permissions carefully to respect user privacy and platform rules.
FCM helps send push notifications to your React Native app.
Request user permission before receiving notifications.
Use messaging().getToken() to get the device token for sending messages.
Practice
Solution
Step 1: Understand FCM's role
FCM is a service designed to send push notifications to apps.Step 2: Compare options
Only To send push notifications to the app users describes sending push notifications, which is FCM's main purpose.Final Answer:
To send push notifications to the app users -> Option CQuick Check:
FCM = Push notifications [OK]
- Confusing FCM with local data storage
- Thinking FCM manages UI or navigation
- Assuming FCM is for database operations
Solution
Step 1: Identify token retrieval method
The method to get the device token is messaging().getToken().Step 2: Eliminate other options
requestPermission() asks user permission, sendMessage() sends messages, subscribeToTopic() subscribes to topics.Final Answer:
messaging().getToken() -> Option BQuick Check:
Get token = getToken() [OK]
- Using requestPermission() to get token
- Confusing sendMessage() with token retrieval
- Mixing subscribeToTopic() with token methods
import messaging from '@react-native-firebase/messaging';
async function getToken() {
const token = await messaging().getToken();
console.log(token);
}
getToken();Solution
Step 1: Analyze async function behavior
The function awaits messaging().getToken(), which returns the device token string.Step 2: Understand console.log output
It logs the token string, not undefined or error, assuming messaging is imported correctly.Final Answer:
Logs the device's FCM token string to the console -> Option AQuick Check:
getToken() returns token string [OK]
- Forgetting await causing Promise logged
- Assuming getToken() returns undefined
- Not importing messaging correctly
import messaging from '@react-native-firebase/messaging'; messaging().requestPermission();What is the likely problem?
Solution
Step 1: Check async method usage
requestPermission() returns a Promise and should be awaited to complete permission request.Step 2: Identify missing await effect
Without await, the permission request may not finish before next steps, causing unexpected behavior.Final Answer:
You forgot to await the requestPermission() call -> Option DQuick Check:
Await async calls like requestPermission() [OK]
- Not awaiting async permission calls
- Thinking requestPermission() is invalid
- Calling getToken() before permission
Solution
Step 1: Check permission before token
const authStatus = await messaging().requestPermission(); if (authStatus === messaging.AuthorizationStatus.AUTHORIZED) { const token = await messaging().getToken(); console.log(token); } requests permission and checks if authorized before getting token.Step 2: Verify async and order correctness
const authStatus = await messaging().requestPermission(); if (authStatus === messaging.AuthorizationStatus.AUTHORIZED) { const token = await messaging().getToken(); console.log(token); } awaits both calls and uses correct AuthorizationStatus check.Final Answer:
const authStatus = await messaging().requestPermission(); if (authStatus === messaging.AuthorizationStatus.AUTHORIZED) { const token = await messaging().getToken(); console.log(token); } -> Option AQuick Check:
Check permission first, then get token [OK]
- Getting token before permission
- Not awaiting async calls
- Ignoring authorization status check
Ready to prove it?
Pass the test and this pattern moves unknown
