-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/fcm-click' into feat/#72
- Loading branch information
Showing
21 changed files
with
3,146 additions
and
306 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link | ||
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" | ||
rel="stylesheet" | ||
/> | ||
<link rel="preconnect" href="https://fonts.googleapis.com"> | ||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:[email protected]&family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet"> | ||
<script type="module" crossorigin src="/assets/index-DNagE171.js"></script> | ||
<link rel="stylesheet" crossorigin href="/assets/index-KWo6zzW6.css"> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { apiClient } from "../instance"; | ||
|
||
interface FcmOffsetUpdateRequest { | ||
token: string; | ||
notificationOffset: number; | ||
} | ||
|
||
type FcmOffsetUpdateResponse = string; | ||
|
||
const useFcmOffsetUpdate = () => { | ||
return useMutation<FcmOffsetUpdateResponse, Error, FcmOffsetUpdateRequest>({ | ||
mutationFn: async (data: FcmOffsetUpdateRequest) => { | ||
// query parameterλ‘ λ³κ²½ | ||
const response = await apiClient.put<FcmOffsetUpdateResponse>( | ||
"/api/fcm/update/notification-offset", | ||
null, // bodyλ nullλ‘ μ€μ | ||
{ | ||
params: { | ||
// query parametersλ‘ μ μ‘ | ||
token: data.token, | ||
notificationOffset: data.notificationOffset, | ||
}, | ||
}, | ||
); | ||
return response.data; | ||
}, | ||
onError: (error) => { | ||
console.error("FCM μλ¦Ό μ€νμ μ λ°μ΄νΈ μ€ν¨:", error); | ||
}, | ||
}); | ||
}; | ||
|
||
export default useFcmOffsetUpdate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { apiClient } from "../instance"; | ||
import { apiClient } from "@/api/instance"; | ||
|
||
interface FcmUpdateRequest { | ||
interface UpdateNotificationEnabledRequest { | ||
token: string; | ||
isNotificatioinEnabled?: boolean; | ||
notificationOffset?: number; | ||
isNotificationEnabled: boolean; | ||
} | ||
|
||
interface FcmUpdateResponse { | ||
message: string; | ||
} | ||
|
||
const useFcmUpdate = () => { | ||
return useMutation<FcmUpdateResponse, Error, FcmUpdateRequest>({ | ||
mutationFn: async (data: FcmUpdateRequest) => { | ||
const response = await apiClient.put<FcmUpdateResponse>( | ||
"/api/fcm/update", | ||
export const useUpdateNotificationEnabled = () => { | ||
return useMutation({ | ||
mutationFn: async ({ | ||
token, | ||
isNotificationEnabled, | ||
}: UpdateNotificationEnabledRequest) => { | ||
const response = await apiClient.put( | ||
`/api/fcm/update/notification-enabled?token=${token}&isNotificationEnabled=${isNotificationEnabled}`, | ||
{}, // λΉ body | ||
{ | ||
token: data.token, | ||
isNotificationEnabled: data.isNotificatioinEnabled ?? true, | ||
notificationOffset: data.notificationOffset ?? 0, | ||
withCredentials: true, | ||
}, | ||
); | ||
|
||
return response.data; | ||
}, | ||
onError: (error) => { | ||
console.error("FCM ν ν° μ λ°μ΄νΈ μ€ν¨:", error); | ||
onError: (error: any) => { | ||
if (error.response?.status === 401) { | ||
console.error("μΈμ¦ μ λ³΄κ° μκ±°λ λ§λ£λμμ΅λλ€."); | ||
} else { | ||
console.error("μλ¦Ό μ€μ μ λ°μ΄νΈ μ€ν¨:", error); | ||
} | ||
}, | ||
}); | ||
}; | ||
|
||
export default useFcmUpdate; | ||
export default useUpdateNotificationEnabled; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { requestForToken, setupOnMessageListener } from "@/api/firebaseConfig"; | ||
import { apiClient } from "@/api/instance"; | ||
|
||
const FCM_TOKEN_KEY = "fcm_token"; | ||
|
||
// λͺ¨λ λ 벨μμ μ μ νλκ·Έ μ μΈ | ||
let isTokenRequestedGlobal = false; | ||
|
||
const useNotificationSetup = () => { | ||
const tokenRequestedRef = useRef(false); | ||
|
||
useEffect(() => { | ||
const initializeFCM = async () => { | ||
// μλ¦Ό API μ§μ μ¬λΆ νμΈ | ||
if (!("Notification" in window)) { | ||
alert("νμ¬ μ¬μ© μ€μΈ λΈλΌμ°μ λ μλ¦Ό κΈ°λ₯μ μ§μνμ§ μμ΅λλ€."); | ||
return; | ||
} | ||
|
||
// μ΄λ―Έ ν ν° μμ²μ΄ μ§ν μ€μ΄κ±°λ μλ£λ κ²½μ° μ€λ³΅ μ€ν λ°©μ§ | ||
if (isTokenRequestedGlobal || tokenRequestedRef.current) { | ||
return; | ||
} | ||
|
||
try { | ||
isTokenRequestedGlobal = true; // μ μ νλκ·Έ μ€μ | ||
tokenRequestedRef.current = true; | ||
|
||
const existingToken = localStorage.getItem(FCM_TOKEN_KEY); | ||
if (!existingToken) { | ||
if (Notification.permission === "denied") { | ||
return; | ||
} | ||
|
||
const permission = await Notification.requestPermission(); | ||
if (permission === "granted") { | ||
const fcmToken = await requestForToken(); | ||
if (fcmToken) { | ||
await apiClient.post("/api/fcm/register", { token: fcmToken }); | ||
localStorage.setItem(FCM_TOKEN_KEY, fcmToken); | ||
console.log("New FCM token registered and saved:", fcmToken); | ||
} | ||
} else if (permission === "denied") { | ||
alert( | ||
"μλ¦Ό κΆνμ΄ κ±°λΆλμμ΅λλ€. λΈλΌμ°μ μ€μ μμ μλ¦Όμ νμ±νν΄μ£ΌμΈμ.", | ||
); | ||
} | ||
} | ||
|
||
// λ©μμ§ λ¦¬μ€λ μ€μ | ||
setupOnMessageListener(); | ||
} catch (error) { | ||
console.error("Failed to initialize FCM:", error); | ||
// μλ¬ λ°μ μ νλκ·Έ μ΄κΈ°ν | ||
isTokenRequestedGlobal = false; | ||
tokenRequestedRef.current = false; | ||
} | ||
}; | ||
|
||
initializeFCM(); | ||
|
||
return () => { | ||
tokenRequestedRef.current = false; | ||
}; | ||
}, []); | ||
|
||
const getFCMToken = () => { | ||
return localStorage.getItem(FCM_TOKEN_KEY); | ||
}; | ||
|
||
return { | ||
getFCMToken, | ||
}; | ||
}; | ||
|
||
export default useNotificationSetup; |
Oops, something went wrong.