React Native course · Module 7: Notifications
Notification Channels (Android)
In this lesson3
Android requires notification channels since version 8.0 (Oreo).
Creating Channels
import * as Notifications from 'expo-notifications';
import { Platform } from 'react-native';
async function setupNotificationChannels() {
if (Platform.OS !== 'android') return;
// Channel for important notifications
await Notifications.setNotificationChannelAsync('urgent', {
name: 'Urgent notifications',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF0000',
sound: 'alarm.wav',
enableVibrate: true,
enableLights: true,
});
// Channel for messages
await Notifications.setNotificationChannelAsync('messages', {
name: 'Messages',
importance: Notifications.AndroidImportance.HIGH,
sound: 'message.wav',
});
// Channel for reminders
await Notifications.setNotificationChannelAsync('reminders', {
name: 'Reminders',
importance: Notifications.AndroidImportance.DEFAULT,
});
// Channel for marketing (low priority)
await Notifications.setNotificationChannelAsync('marketing', {
name: 'Promotions and news',
importance: Notifications.AndroidImportance.LOW,
});
}Using Channels
// Sending to a specific channel
await Notifications.scheduleNotificationAsync({
content: {
title: 'Urgent message!',
body: 'The Atlantis security system has been breached!',
},
trigger: null,
// Specify channel for Android
...(Platform.OS === 'android' && {
channelId: 'urgent',
}),
});Managing Channels
// Get all channels
const channels = await Notifications.getNotificationChannelsAsync();
// Get specific channel
const channel = await Notifications.getNotificationChannelAsync('urgent');
// Delete channel
await Notifications.deleteNotificationChannelAsync('old-channel');Channels allow users to control different types of notifications!
Code for this lesson: App.tsx
import { useEffect } from 'react';
import { View, Text, StyleSheet, Platform } from 'react-native';
import * as Notifications from 'expo-notifications';
// Configure notification channels for Android (since Android 8.0 Oreo)
async function setupNotificationChannels() {
if (Platform.OS !== 'android') return;
// TODO: create the 'urgent' channel with MAX importance
// use Notifications.setNotificationChannelAsync('urgent', { name, importance })
await Notifications.setNotificationChannelAsync('urgent', {
name: 'Urgent notifications',
// TODO: set importance to Notifications.AndroidImportance.MAX
importance: Notifications.AndroidImportance.DEFAULT,
});
// TODO: create the 'reminders' channel with HIGH importance
// await Notifications.setNotificationChannelAsync('reminders', { ... });
// TODO: create the 'marketing' channel with LOW importance
// await Notifications.setNotificationChannelAsync('marketing', { ... });
}
export default function App() {
useEffect(() => {
setupNotificationChannels();
}, []);
return (
<View style={styles.container}>
<Text style={styles.title}>Android Channels</Text>
<Text style={styles.info}>urgent • reminders • marketing</Text>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#1a5276' },
title: { fontSize: 24, color: '#fff', marginBottom: 12 },
info: { fontSize: 14, color: '#aed6f1' },
});
Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. From which version of Android are notification channels required?
2. Which importance value gives the highest priority?
Hands-on tasks in the game
- Click in order
Click the channel creation elements:
- Code editor
Complete the setNotificationChannelAsync code.