What is expo-notifications?
The expo-notifications package is a library for handling notifications in Expo and React Native applications. It provides tools for scheduling, receiving, and managing both local and push notifications.
What are expo-notifications's main functionalities?
Scheduling Local Notifications
This feature allows you to schedule local notifications to be triggered at a specific time or after a certain delay.
import * as Notifications from 'expo-notifications';
async function scheduleNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "You've got mail! π¬",
body: 'Here is the notification body',
},
trigger: { seconds: 2 },
});
}
Handling Received Notifications
This feature allows you to handle notifications when they are received, enabling you to perform actions or update the UI in response to the notification.
import * as Notifications from 'expo-notifications';
import { useEffect } from 'react';
useEffect(() => {
const subscription = Notifications.addNotificationReceivedListener(notification => {
console.log(notification);
});
return () => subscription.remove();
}, []);
Requesting Permissions
This feature allows you to request the necessary permissions from the user to send notifications.
import * as Notifications from 'expo-notifications';
async function requestPermissions() {
const { status } = await Notifications.requestPermissionsAsync();
if (status !== 'granted') {
alert('Permission not granted!');
}
}
Customizing Notification Behavior
This feature allows you to customize how notifications are handled when they are received, such as whether to show an alert, play a sound, or set a badge.
import * as Notifications from 'expo-notifications';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
Other packages similar to expo-notifications
react-native-push-notification
react-native-push-notification is a popular library for handling push notifications in React Native applications. It offers similar functionalities to expo-notifications, such as scheduling local notifications, handling received notifications, and requesting permissions. However, it requires more manual setup and configuration compared to expo-notifications, which is more integrated with the Expo ecosystem.
notifee
notifee is a powerful notification library for React Native that provides advanced features like notification channels, grouped notifications, and custom notification sounds. It offers more customization options compared to expo-notifications but also requires more setup and configuration.
react-native-firebase
react-native-firebase is a comprehensive library that includes support for Firebase Cloud Messaging (FCM) to handle push notifications. It provides robust features for managing notifications, including background message handling and analytics. It is more complex to set up compared to expo-notifications but offers deeper integration with Firebase services.
Provides an API to fetch push notification tokens and to present, schedule, receive and respond to notifications.
Features
- π£ schedule a one-off notification for a specific date, or some time from now,
- π schedule a notification repeating in some time interval (or a calendar date match on iOS),
- 1οΈβ£ get and set application badge icon number,
- π² fetch a native device push token so you can send push notifications with FCM and APNS,
- π fetch an Expo push token so you can send push notifications with Expo,
- π¬ listen to incoming notifications in the foreground and background,
- π listen to interactions with notifications (tapping or dismissing),
- π handle notifications when the app is in foreground,
- π imperatively dismiss notifications from Notification Center/tray,
- π create, update, delete Android notification channels,
- π¨ set custom icon and color for notifications on Android.
Guides
Please refer to our notification guides.
API documentation