From 045785f1e19fde2397bc92c65a56faf8035c075e Mon Sep 17 00:00:00 2001 From: jnkeniaem Date: Tue, 26 Nov 2024 17:22:35 +0900 Subject: [PATCH] =?UTF-8?q?[FE]=20ETC:=20firebase=20messaging=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=ED=9B=84=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9B=90=EB=9E=98=EB=8C=80=EB=A1=9C=20=EB=B3=B5?= =?UTF-8?q?=EA=B5=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Cabinet/firebase/firebase-messaging-sw.ts | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/frontend/src/Cabinet/firebase/firebase-messaging-sw.ts b/frontend/src/Cabinet/firebase/firebase-messaging-sw.ts index 0509f1301..fdab56028 100644 --- a/frontend/src/Cabinet/firebase/firebase-messaging-sw.ts +++ b/frontend/src/Cabinet/firebase/firebase-messaging-sw.ts @@ -24,33 +24,32 @@ let messaging: null | Messaging = null; let isApiSupported = false; // NOTE : 사용자 브라우저가 푸시 알림 기능을 지원하는지 확인 -// isSupported().then((result) => { -// isApiSupported = result; -// if ( -// typeof window !== "undefined" && -// typeof window.navigator !== "undefined" && -// isApiSupported -// ) { -// messaging = getMessaging(app); -// } -// }); -messaging = getMessaging(app); +isSupported().then((result) => { + isApiSupported = result; + if ( + typeof window !== "undefined" && + typeof window.navigator !== "undefined" && + isApiSupported + ) { + messaging = getMessaging(app); + } +}); const unsupportedMsg = `사용 중인 환경에서는 푸시 알림 기능이 지원되지 않습니다. 데스크탑 이용을 권장드립니다.`; -// const checkBrowserSupport = () => { -// if (!isApiSupported) { -// let error = new Error(unsupportedMsg); -// error.name = "브라우저 알림 지원 제한"; -// throw error; -// } -// }; +const checkBrowserSupport = () => { + if (!isApiSupported) { + let error = new Error(unsupportedMsg); + error.name = "브라우저 알림 지원 제한"; + throw error; + } +}; // FCM APP을 등록 후 브라우저 알림 권한을 요청하고, 토큰을 반환 export const requestFcmAndGetDeviceToken = async (): Promise => { - // checkBrowserSupport(); + checkBrowserSupport(); console.log("권한 요청 중..."); const permission = await Notification.requestPermission(); @@ -61,16 +60,14 @@ export const requestFcmAndGetDeviceToken = async (): Promise => { console.log("알림 권한이 허용됨"); - // const token = await getToken(messaging!, { - const token = await getToken(messaging, { + const token = await getToken(messaging!, { vapidKey: import.meta.env.VITE_FIREBASE_APP_VAPID_KEY, }); if (token) console.log("token: ", token); else console.log("Can not get Token"); - // onMessage(messaging!, (payload) => { - onMessage(messaging, (payload) => { + onMessage(messaging!, (payload) => { console.log("메시지가 도착했습니다.", payload); // ... }); @@ -80,9 +77,8 @@ export const requestFcmAndGetDeviceToken = async (): Promise => { // FCM 토큰 제거 및 브라우저 알람 권한 해제 export const deleteFcmToken = async (): Promise => { - // checkBrowserSupport(); + checkBrowserSupport(); - // await deleteToken(messaging!); - await deleteToken(messaging); + await deleteToken(messaging!); console.log("Token deleted."); };