문제 상황을 알려주세요!
문제가 된 코드
if ('Notification' in window) {
Notification.requestPermission().then((newPermission) => {
setPermission(newPermission);
});
}
에러 상황
- iOS에서
Notification.requestPermission()
을 호출했을 때, 특정 환경에서 푸시 알림이 제대로 동작하지 않음
- PWA(Progressive Web App) 환경이 아닐 때 iOS에서
Notification
을 사용할 수 없음
문제 원인이 무엇인가요?
에러가 발생한 이유
- iOS에서는 PWA(홈 화면에 추가된 상태)에서만
Notification
API를 사용할 수 있음
- 일반 브라우저(Safari, Chrome)에서는
Notification
이 지원되지 않음
- 따라서
Notification.requestPermission()
을 호출하면 iOS에서 제대로 동작하지 않거나 오류가 발생할 가능성이 있음
해결
해결 된 코드
const isPWA = window.matchMedia('(display-mode: standalone)').matches;
if ('Notification' in window || isPWA) {
Notification.requestPermission().then((newPermission) => {
setPermission(newPermission);
});
}
해결 방법 설명
window.matchMedia('(display-mode: standalone)').matches
를 사용해 PWA 환경인지 확인
- PWA 환경이 아닐 경우, iOS에서
Notification
API를 호출하지 않도록 처리