Local notifications
- React Native course
- Advanced
- Lesson 3/83
- 5 min
Learn to send local notifications.
Local notification
A local notification is scheduled by the app itself. No server is needed; all scheduling happens on the device. It is ideal for reminders, alarms and timed tasks.
A local notification is scheduled with Notifications.scheduleNotificationAsync. This function takes a content field (title, body, data) and a trigger field (when to show it). For example trigger: {{ seconds: 10 }} shows the notification ten seconds later.
Content and scheduling
The content field of a notification is an object. title is the notification's heading and body is the body text. data is a free object you can attach; when the user taps the notification you read this object.
For scheduling, trigger takes an object. The simplest option is { { seconds: 10 } } — the notification is shown ten seconds later. For a specific date you can pass a Date object to the date property. For repeating notifications, set repeats to true.
Practice Schedule a local notification
Complete the empty function. Call Notifications.scheduleNotificationAsync. The first argument is an object: content: {{ title: 'Reminder', body: 'Time is up' }} and trigger: {{ seconds: 5 }}. Await the call.
Starter code
import * as Notifications from 'expo-notifications';
export async function scheduleReminder() {
// schedule a notification five seconds from now
}Sample solution
Notifications.scheduleNotificationAsync({
content: { title: 'Reminder', body: 'Time is up' },
trigger: { seconds: 5 },
});Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match each field to its purpose
| Term | Meaning |
|---|---|
content |
Notification content |
trigger |
When to show |
title |
Notification title |
seconds |
Delay duration |
2 Write the content object
content: { ____: 'Hatırlatma', body: 'Vakti geldi' }
content: { title: 'Hatırlatma', body: 'Vakti geldi' }
Explanation: title is the notification heading.
3 When is a local notification shown?
- Only when the app gets a signal from the server
- Only when the device connects to the internet
- At the time given by the trigger field Correct answer
- Only while the app is in the foreground
Explanation: The trigger field sets the time; no server is needed.