
Learn how to set up, configure, and handle push notifications in Expo React Native apps for both Android and iOS devices.
Push notifications are one of the most reliable ways to keep users engaged with your mobile app. Whether you are building a real-time chat app, a fitness tracker like Nept Track, or a logistics platform, sending timely alerts directly to a user's phone makes a massive difference in retention.
Setting up push notifications used to be a frustrating chore involving APNs certificates, FCM server keys, and native configuration files. Thankfully, Expo's push notification service and the expo-notifications library make this process much smoother for cross-platform apps.
In this guide, I will walk you through setting up push notifications in a React Native and Expo project, handling tokens, managing permissions, and displaying alerts correctly on both Android and iOS.
Before diving into the code, it helps to understand how Expo simplifies the architecture. Usually, you would need to set up Firebase Cloud Messaging (FCM) for Android and Apple Push Notification service (APNs) for iOS separately.
Expo provides a unified wrapper and an HTTP v2 push server. You send a standard POST request to Expo's push server from your backend, and Expo handles the heavy lifting of talking to Apple and Google behind the scenes. This saves hours of debugging configuration issues.
First, install the core notification library and device info library in your Expo project:
npx expo install expo-notifications expo-device
expo-notifications manages permissions, tokens, and incoming listeners, while expo-device helps us check if the app is running on a physical device, as push notifications do not work on most iOS simulators.
Next, you need to configure your app.json file to include necessary plugin settings and icons. Android requires a notification icon and a color tint.
{
"expo": {
"name": "My App",
"slug": "my-app",
"ios": {
"bundleIdentifier": "com.example.myapp"
},
"android": {
"package": "com.example.myapp",
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
}
},
"plugins": [
[
"expo-notifications",
{
"icon": "./assets/notification-icon.png",
"color": "#ffffff",
"sounds": []
}
]
]
}
}
Tagged with
Leave a comment
Make sure your notification icon is a single-color silhouette with a transparent background, as required by modern Android guidelines.
Push notification tokens are unique to each device and user installation. You must request permission from the user before generating this token.
Here is a practical hook or helper function you can run when your app starts up:
import { Platform } from 'react-native';
import * as Device from 'expo-device';
import * as Notifications from 'expo-notifications';
import Constants from 'expo-constants';
export async function registerForPushNotificationsAsync() {
let token;
if (Platform.OS === 'android') {
await Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
if (Device.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
try {
const projectId =
Constants?.expoConfig?.extra?.eas?.projectId ??
Constants?.easConfig?.projectId;
token = (await Notifications.getExpoPushTokenAsync({ projectId })).data;
console.log('Expo Push Token:', token);
} catch (e) {
console.error('Error fetching push token', e);
}
} else {
alert('Must use physical device for Push Notifications');
}
return token;
}
Save this token to your database associated with the logged-in user so your server knows where to send alerts later.
To handle notifications when the app is running in the foreground, background, or completely closed, configure your notification behavior early in your app lifecycle (like in App.js or _layout.tsx for Expo Router).
import { useEffect, useRef } from 'react';
import * as Notifications from 'expo-notifications';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
export default function usePushNotifications() {
const notificationListener = useRef();
const responseListener = useRef();
useEffect(() => {
// This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
console.log('Notification Received: ', notification);
});
// This listener is fired whenever a user taps on or interacts with a notification
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log('Notification Clicked: ', response);
const data = response.notification.request.content.data;
// Navigate based on data payload
if (data?.screen) {
// router.push(data.screen);
}
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
}
Once you have the Expo push token, testing is straightforward. You can send a test notification using a simple fetch request from your Node.js backend or even directly via curl:
curl -X POST https://exp.host/--/api/v2/push/send \
-H "Content-Type: application/json" \
-d '{
"to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"title": "Hello from Nazmul",
"body": "This is a test push notification!",
"data": { "screen": "settings" }
}'
If configured correctly, your device will display the notification instantly.
When moving your Expo app to production, keep a few important points in mind:
eas credentials).Adding push notifications to an Expo React Native app doesn't have to be painful. By leaning on Expo's managed workflow and expo-notifications, you can implement robust cross-platform alerts with minimal native code configuration.
Need help building a custom mobile app or integrating reliable push notification workflows for your business? Get in touch and let's talk about your project.