Skip to content

Commit

Permalink
fix: fcm debugging
Browse files Browse the repository at this point in the history
+ refactoring
  • Loading branch information
rabyeoljji committed Nov 27, 2024
1 parent a7281c6 commit a9831cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/firebase/messaging/use-service-worker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ export const useServiceWorker = () => {
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/firebase-messaging-sw.js')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.then((registration) => {
// eslint-disable-next-line no-console
console.log('Service Worker 등록 성공:', registration)
// console.log('Service Worker 등록 성공:', registration) // 디버깅용
})
.catch((error) => {
console.error('Service Worker 등록 실패:', error)
Expand Down
12 changes: 12 additions & 0 deletions src/requests/fcm/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use client'

import { useMutation } from '@tanstack/react-query'
import { postFcmToken } from '.'

export const usePostFcmToken = () => {
return useMutation({
mutationFn: async (fcmToken: string) => postFcmToken({ fcmToken }),
// eslint-disable-next-line no-console
onSuccess: () => console.log('토큰 전송 성공'),
})
}
23 changes: 23 additions & 0 deletions src/requests/fcm/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use server'

import { auth } from '@/app/api/auth/[...nextauth]/auth'
import { API_ENDPOINTS } from '@/shared/configs/endpoint'
import { http } from '@/shared/lib/axios/http'

export const postFcmToken = async (requestBody: { fcmToken: string }) => {
try {
const session = await auth()

const response = await http.post(API_ENDPOINTS.FCM.POST.TOKEN, requestBody, {
headers: {
Authorization: `Bearer ${session?.user.accessToken}`,
},
})

// eslint-disable-next-line no-console
console.log(response) // 디버깅용
} catch (error) {
console.error(error)
throw error
}
}
22 changes: 6 additions & 16 deletions src/shared/hooks/use-messaging.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,29 @@
import { getToken } from '@/firebase/messaging/get-token'
import { useServiceWorker } from '@/firebase/messaging/use-service-worker'
import { useEffect } from 'react'
import { http } from '../lib/axios/http'
import { API_ENDPOINTS } from '../configs/endpoint'
import { useSession } from 'next-auth/react'
import { usePostFcmToken } from '@/requests/fcm/hooks'

export const useMessaging = () => {
useServiceWorker()

const { data: session } = useSession()
const { mutate: postFcmTokenMutate } = usePostFcmToken()

useEffect(() => {
try {
const requestFCMToken = async () => {
const token = await getToken()

if (token) {
// eslint-disable-next-line no-console
console.log('FCM 토큰:', token) // 디버깅용
// console.log('FCM 토큰:', token) // 디버깅용

// FCM 토큰을 서버로 전송
await http.post(
API_ENDPOINTS.FCM.POST.TOKEN,
{ fcmToken: token },
{
headers: {
Authorization: `Bearer ${session?.user.accessToken}`,
},
}
)
postFcmTokenMutate(token)
}
}

void requestFCMToken()
} catch (error) {
console.error(error)
}
}, [session])
}, [postFcmTokenMutate])
}

0 comments on commit a9831cf

Please sign in to comment.