inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

React에서 FCM 사용

1132

김민지

작성한 질문수 1

0

안녕하세요 !

FCM으로 푸시 기능을 짜다가,, 도저히 모르겠어서 글 남깁니다 ..!

 

문제는 콘솔에는 푸시 알림이 왔다는 게 찍히는데,

푸시가 보이지 않습니다 ... ㅠㅠ

[ 폴더 구조 ]

[ firebase-messaging-sw.js ]

self.addEventListener('push', function (e) {
  console.log('push: ', e.data.json())
  if (!e.data.json()) return

  const resultData = e.data.json().notification
  const notificationTitle = resultData.title
  const notificationOptions = {
    body: resultData.body,
    icon: resultData.image,
    tag: resultData.tag,
    ...resultData
  }
  console.log('push: ', { resultData, notificationTitle, notificationOptions })

  registration.showNotification(notificationTitle, notificationOptions)
})

[ messaging_init_in_sw.js ]

import { initializeApp } from 'firebase/app'
import { getMessaging, getToken } from 'firebase/messaging'

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
  appId: process.env.REACT_APP_FIREBASE_APP_ID,
  measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID
}

function requestPermission() {
  console.log('푸시 허가 받는 중 ...')

  void Notification.requestPermission().then((permission) => {
    if (permission === 'granted') {
      console.log('푸시 알림이 허용되었습니다.')
    } else {
      console.log('푸시 알림이 허용되지 않았습니다')
    }
  })

  const app = initializeApp(firebaseConfig)
  const messaging = getMessaging(app)

  void getToken(messaging, { vapidKey: process.env.REACT_APP_FIREBASE_VAPID_KEY }).then((token) => {
    if (token.length > 0) {
      console.log('푸시 토큰 : ', token)
    } else {
      console.log('푸시 토큰 실패 !')
    }
  })
}

requestPermission()

 

토큰은 제대로 잘 받아와지는데,,, 혹시 서비스워커 코드에 문제가 있는 걸까요 ..?

react fcm firebase push

답변 1

0

김민지

image

혹시 localhost에서는 불가능한건가요 ..?!

강사님 claude.md 가 궁금합니다.

0

8

1

Reducer undefined로 나오는 문제의 건

0

29

2

자꾸 명령하면 파워쉘이 꺼져요.

0

26

2

좀 정신이 없는듯 합니다.

0

41

1

Vs code에서 Open editor window 설정 확인 방법

0

29

1

36~42강 강의 따라하면서 들어야되나요?

0

37

2

install code command 검색 시 안뜸

0

30

2

zod 관련

0

23

1

windows power shell 에서 어떠한 기본 설명도 없이 linux 명령 ls 를 이야기 하면

0

30

2

[(5.5) supabase] shadcn 토글(on/off)

0

29

1

클로드코드의 툴

0

38

0

오리엔테이션 Q1.

0

41

2

클로드코드 왼쪽 위 탭 활용.

0

47

2

다중 터미널의 용도

1

42

1

Sequential Thinking API Error

0

58

1

회원 가입 폼 실습에서 value? 개념이 헷갈려요

0

50

2

컴맹도 들을수있는거맞아요?..

0

68

2

꼭 스탭 순서대로 봐야 하나요?

0

53

2

스탭마다 새로 만들면 안되나요?

0

46

2

cursor 에서 붙여넣기 옵션

0

36

2

login expired. please run / login 메세지가 뜹니다.

0

43

1

교안자료 다운로드 방법 문의

0

34

1

노드 js 관련 질문이 있습니다

0

50

2

자료 관련 질문?

0

41

1