Push Notifications
Send and receive push notifications and local notifications to engage users.
Learning Goals
The Core Concept
Notifications are a powerful way to re-engage users. Local notifications are triggered by the app itself (e.g., a timer finishing), while Push notifications are sent from a remote server. Expo provides the `expo-notifications` module which simplifies requesting permissions and scheduling local or handling push notifications across both iOS and Android.
Visual guide
React-native concept flow
A simple original diagram to connect the lesson idea with real project flow.
Code & Implementation
import React, { useEffect } from 'react';
import { View, Button, Alert } from 'react-native';
import * as Notifications from 'expo-notifications';
// Determine how notifications should behave when app is foregrounded
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
export default function NotificationApp() {
useEffect(() => {
// Request permission
const requestPermissions = async () => {
const { status } = await Notifications.requestPermissionsAsync();
if (status !== 'granted') {
Alert.alert('Permission to send notifications was denied');
}
};
requestPermissions();
}, []);
const triggerLocalNotification = async () => {
await Notifications.scheduleNotificationAsync({
content: {
title: "Hello! 📬",
body: "This is a local notification triggered from code.",
},
trigger: { seconds: 2 }, // Trigger after 2 seconds
});
};
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button
title="Trigger Notification (2s delay)"
onPress={triggerLocalNotification}
/>
</View>
);
}Expected Output
A simple screen with a button. Pressing it triggers a native system notification drop-down 2 seconds later.
AMC Reminder App
Hands-on practice task
The Challenge
An app that lets users schedule local notifications to remind them to renew their Annual Maintenance Contracts.
Helpful Hints
- •Create a form taking an item name and a date.
- •Calculate the time difference in seconds.
- •Use expo-notifications to schedule a notification for that future date.
Quick Knowledge Check
Do notifications work in the iOS simulator?
Continue Learning
Next steps after this lesson
An app that lets users schedule local notifications to remind them to renew their Annual Maintenance Contracts.
Supercharge your career workflows!
Discover free online utilities to format data, build job applications, and automate your productivity routine with TechIdea.