1---
2title: Receive notifications
3description: Learn how to respond to a notification received by your app and take action based on the event.
4---
5
6import { BoxLink } from '~/ui/components/BoxLink';
7
8The [`expo-notifications`](/versions/latest/sdk/notifications) library contains event listeners that handle how your app responds when receiving a notification.
9
10## Notification event listeners
11
12The `addNotificationReceivedListener` and `addNotificationResponseReceivedListener` event listeners receive an object when a notification is received or interacted with.
13
14These listeners allow you to add behavior when notifications are received while your app is open and foregrounded and when your app is backgrounded or closed and the user taps on the notification.
15
16```js
17useEffect(() => {
18  registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
19
20  /* @info This listener is fired whenever a notification is received while the app is foregrounded. */
21  notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
22    setNotification(notification);
23  });
24  /* @end */
25
26  /* @info This listener is fired whenever a user taps on or interacts with a notification (works when an app is foregrounded, backgrounded, or killed). */
27  responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
28    console.log(response);
29  });
30  /* @end */
31
32  return () => {
33    Notifications.removeNotificationSubscription(notificationListener.current);
34    Notifications.removeNotificationSubscription(responseListener.current);
35  };
36}, []);
37```
38
39For more information on these objects, see [`Notification`](/versions/latest/sdk/notifications/#notification) documentation.
40
41## Foreground notification behavior
42
43To handle the behavior when notifications are received when your app is **foregrounded**, use [`Notifications.setNotificationHandler`](/versions/latest/sdk/notifications/#handling-incoming-notifications-when-the-app-is) with the `handleNotification()` callback to set the following options:
44
45- `shouldShowAlert`
46- `shouldPlaySound`
47- `shouldSetBadge`
48
49```jsx
50Notifications.setNotificationHandler({
51  handleNotification: async () => ({
52    shouldShowAlert: true,
53    shouldPlaySound: false,
54    shouldSetBadge: false,
55  }),
56});
57```
58
59## Closed notification behavior
60
61On Android, users can set certain OS-level settings, usually revolving around performance and battery optimization, that can prevent notifications from being delivered when the app is closed. For example, one such setting is the **Deep Clear** option on OnePlus devices using Android 9 and lower versions.
62
63## Next
64
65<BoxLink
66  title="Common questions and FAQs"
67  description="A collection of common questions about Expo's push notification service."
68  href="/push-notifications/faq"
69/>
70