expo-notifications 는 Expo 앱에서 알림을 관리하는데 사용되는 라이브러리 입니다. 이 라이브러리를 사용하면 로컬 알림과 푸시 알림을 쉽게 설정하고 관리할 수 있습니다.
expo install expo-notifications
설치가 완료되면, 알림을 설정하고 정보를 조회하는 코드를 작성해 보겠습니다.
import * as Notifications from 'expo-notifications';
import { Button } from 'react-native';
// 알림을 요청하는 함수
async function requestNotificationPermissions() {
const { status } = await Notifications.requestPermissionsAsync();
if (status !== 'granted') {
alert('알림 권한이 필요합니다!');
return;
}
}
// 로컬 알림을 설정하는 함수
async function scheduleNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "알림 제목",
body: "여기에 알림 내용을 입력하세요.",
},
trigger: {
seconds: 10, // 10초 후에 알림이 발생
// 바로 하고싶은경우 trigger: null,
},
});
}
// 버튼 클릭 시 알림을 요청하고 설정
<Button title="알림 설정" onPress={async () => {
await requestNotificationPermissions();
await scheduleNotification();
}} />
하지만 알림을 위해서는 권한이 필요합니다. 알림 권한을 요청하는 함수를 작성 해볼까요 ?
아래 키워드에 대해서 검색해본 후 , 각자 작성해보아요.
Notifications.requestPermissionsAsync()