--- title: Push notifications setup sidebar_title: Setup description: Learn how to setup push notifications, get credentials for development and production, and test sending push notifications. --- import { Tab, Tabs } from '~/ui/components/Tabs'; import { Terminal } from '~/ui/components/Snippet'; import { Step } from '~/ui/components/Step'; import ImageSpotlight from '~/components/plugins/ImageSpotlight'; import { BoxLink } from '~/ui/components/BoxLink'; To use Expo's push notification service, you need to configure your app to install a set of libraries, add functions to handle notifications and configure credentials for Android and iOS. After going through these steps, you'll be able to test notification sending and receiving notifications on a device. To get the client-side ready for push notifications, the following things are required: - The user's permission to send them push notifications. - The user's [`ExpoPushToken`](/versions/latest/sdk/notifications/#expopushtoken). ## Install libraries Run the following command to install `expo-notifications` and `expo-device` libraries: [`expo-notifications`](/versions/latest/sdk/notifications) library is used to request for a user's permission and to fetch the `ExpoPushToken`. It is not supported on an Android Emulator or an iOS simulator. The [`expo-device`](/versions/latest/sdk/device) is used to check whether the app is running on a physical device. ## Add a minimal working example The code below shows a working example of how to register for, send, and receive push notifications in a React Native app. Copy and paste it into your project: ```jsx App.js import { useState, useEffect, useRef } from 'react'; import { Text, View, Button, Platform } from 'react-native'; import * as Device from 'expo-device'; import * as Notifications from 'expo-notifications'; /* @info This handler determines how your app handles notifications that come in while the app is foregrounded. */ Notifications.setNotificationHandler({ handleNotification: async () => ({ shouldShowAlert: true, shouldPlaySound: false, shouldSetBadge: false, }), }); /* @end */ // Can use this function below OR use Expo's Push Notification Tool from: https://expo.dev/notifications async function sendPushNotification(expoPushToken) { const message = { to: expoPushToken, sound: 'default', title: 'Original Title', body: 'And here is the body!', data: { someData: 'goes here' }, }; await fetch('https://exp.host/--/api/v2/push/send', { method: 'POST', headers: { Accept: 'application/json', 'Accept-encoding': 'gzip, deflate', 'Content-Type': 'application/json', }, body: JSON.stringify(message), }); } async function registerForPushNotificationsAsync() { let token; /* @info You should make sure the app is running on a physical device since push notifications don't work on an emulator/simulator. */ if (Device.isDevice) { /* @end */ 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; } /* @info This provides the ExpoPushToken. */ token = (await Notifications.getExpoPushTokenAsync()).data; /* @end */ console.log(token); } else { alert('Must use physical device for Push Notifications'); } /* @info On Android, you need to specify a channel. */ if (Platform.OS === 'android') { Notifications.setNotificationChannelAsync('default', { name: 'default', importance: Notifications.AndroidImportance.MAX, vibrationPattern: [0, 250, 250, 250], lightColor: '#FF231F7C', }); } /* @end */ return token; } export default function App() { const [expoPushToken, setExpoPushToken] = useState(''); const [notification, setNotification] = useState(false); const notificationListener = useRef(); const responseListener = useRef(); useEffect(() => { registerForPushNotificationsAsync().then(token => setExpoPushToken(token)); /* @info This listener is fired whenever a notification is received while the app is foregrounded. */ notificationListener.current = Notifications.addNotificationReceivedListener(notification => { setNotification(notification); }); /* @end */ /* @info This listener is fired whenever a user taps on or interacts with a notification (works when an app is foregrounded, backgrounded, or killed). */ responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { console.log(response); }); /* @end */ return () => { Notifications.removeNotificationSubscription(notificationListener.current); Notifications.removeNotificationSubscription(responseListener.current); }; }, []); return ( Your expo push token: {expoPushToken} Title: {notification && notification.request.content.title} Body: {notification && notification.request.content.body} Data: {notification && JSON.stringify(notification.request.content.data)}