-
Notifications
You must be signed in to change notification settings - Fork 0
[Fix] 알림 권한 미허용 시 재요청 로직 추가 #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { Stack, useRouter } from 'expo-router'; | ||
| import { useState, useEffect } from 'react'; | ||
| import { AppState } from 'react-native'; | ||
| import { getForceUpdate, appVersion, versionToNumber } from '../services/forceupdate'; | ||
| import * as Notifications from 'expo-notifications'; | ||
| import { registerForPushNotificationsAsync } from '../services/notifications'; | ||
| import { registerForPushNotificationsAsync, shouldRecheckPermission } from '../services/notifications'; | ||
| import CookieManager from '@react-native-cookies/cookies'; | ||
|
|
||
| Notifications.setNotificationHandler({ | ||
|
|
@@ -32,17 +33,33 @@ export default function RootLayout() { | |
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!isReady) return; | ||
| const handleToken = (token?: string) => { | ||
| if (token) { | ||
| addTokenToCookie(token); | ||
| console.log('Expo Push Token:', token); | ||
| } | ||
| }; | ||
|
|
||
| registerForPushNotificationsAsync() | ||
| .then((token) => { | ||
| if (token) { | ||
| addTokenToCookie(token); | ||
| console.log('Expo Push Token:', token); | ||
| } | ||
| }) | ||
| .then(handleToken) | ||
| .catch((error: any) => console.error(error)); | ||
|
|
||
| const subscription = AppState.addEventListener('change', (nextAppState) => { | ||
| if (nextAppState === 'active' && shouldRecheckPermission()) { | ||
| registerForPushNotificationsAsync() | ||
| .then(handleToken) | ||
| .catch((error: any) => console.error(error)); | ||
| } | ||
| }); | ||
|
Comment on lines
+47
to
+53
|
||
|
|
||
| return () => { | ||
| subscription.remove(); | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!isReady) return; | ||
|
|
||
| const response = Notifications.getLastNotificationResponse(); | ||
| if (response?.notification) { | ||
| const data = response.notification.request.content.data.path; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,26 @@ | ||
| import { Platform } from 'react-native'; | ||
| import { Alert, Linking, Platform } from 'react-native'; | ||
| import * as Device from 'expo-device'; | ||
| import * as Notifications from 'expo-notifications'; | ||
| import Constants from 'expo-constants'; | ||
| import { exitApp } from '@logicwind/react-native-exit-app'; | ||
|
|
||
| let wentToSettings = false; | ||
|
||
|
|
||
| const notificationPermissionAlert = () => | ||
| Alert.alert('알림 권한이 필요해요', '설정으로 이동해서 알림 권한을 허용해주세요.', [ | ||
| { | ||
| text: '앱 종료', | ||
| onPress: () => exitApp(), | ||
| style: 'cancel', | ||
| }, | ||
| { | ||
| text: '확인', | ||
| onPress: () => { | ||
| wentToSettings = true; | ||
| Linking.openSettings(); | ||
| }, | ||
| }, | ||
| ]); | ||
|
Comment on lines
+18
to
+23
|
||
|
|
||
| function handleRegistrationError(errorMessage: string) { | ||
| console.error(errorMessage); | ||
|
|
@@ -24,7 +43,7 @@ export async function registerForPushNotificationsAsync() { | |
| finalStatus = status; | ||
| } | ||
| if (finalStatus !== 'granted') { | ||
| handleRegistrationError('Permission not granted to get push token for push notification!'); | ||
| notificationPermissionAlert(); | ||
|
||
| return; | ||
| } | ||
| const projectId = | ||
|
|
@@ -47,3 +66,11 @@ export async function registerForPushNotificationsAsync() { | |
| handleRegistrationError('Must use physical device for push notifications'); | ||
| } | ||
| } | ||
|
|
||
| export function shouldRecheckPermission() { | ||
| if (wentToSettings) { | ||
| wentToSettings = false; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function
shouldRecheckPermission()is stateful and consumes the flag when called. However, it's called inline in the AppState event handler condition. If this condition is evaluated multiple times during the same render cycle or if multiple listeners exist, this could lead to the flag being consumed unexpectedly. Consider checking the flag and storing the result in a variable before using it in the condition, or refactoring to make the state management more explicit.