
이전 포스팅에서 기본적인 푸시알림 권한 요청과 토큰 관리에 대해 다뤘습니다.
이번에는 사용자가 앱 내에서 푸시알림을 켜고 끌 수 있는 설정 기능과, 로그아웃/회원탈퇴 시의 토큰 관리에 대해 자세히 알아보겠습니다.
토큰 관리의 복잡성
상태 유지의 어려움
에러 처리의 중요성
const NotificationSettingsModal = ({ isVisible, onClose }: TNotificationSettingsModal) => {
const [isEnabled, setIsEnabled] = useState(false);
useEffect(() => {
checkNotificationStatus();
}, []);
const checkNotificationStatus = async () => {
try {
const { status } = await Notifications.getPermissionsAsync();
const savedSettings = await AsyncStorage.getItem("notificationSettings");
setIsEnabled(status === "granted" && savedSettings === "true");
} catch (error) {
console.error("Failed to check notification status:", error);
}
};
// ... 토글 로직
};
const toggleNotifications = async () => {
try {
if (!isEnabled) {
// 알림 활성화 로직
const { status } = await Notifications.requestPermissionsAsync();
if (status === "granted") {
const token = await getExpoPushToken();
await registerDeviceToken(token);
await saveNotificationSettings(true);
}
} else {
// 알림 비활성화 로직
const token = await getExpoPushToken();
await unregisterDeviceToken(token);
await saveNotificationSettings(false);
}
} catch (error) {
handleToggleError(error);
}
};
const handleWithdrawal = async () => {
try {
await withdrawMembership(userId);
// 푸시 알림 관련 설정 초기화
await AsyncStorage.multiRemove([
"pushPermissionShown",
"notificationSettings"
]);
handleLogout();
} catch (error) {
console.error("회원 탈퇴 중 오류 발생", error);
}
};
const handleToggleError = async (error: any) => {
if (error.response?.status === 409) {
// 이미 등록된 토큰
await AsyncStorage.setItem("notificationSettings", "true");
setIsEnabled(true);
} else if (error.response?.status === 404) {
// 이미 삭제된 토큰
await AsyncStorage.setItem("notificationSettings", "false");
setIsEnabled(false);
} else {
Alert.alert("알림 설정 실패", "다시 시도해주세요.");
}
};
디바이스 vs 계정
상태 동기화
사용자 경험