♠♠기술 구현

FCM관련 여러가지 엮인것들 정리

흑백 개발자 2024. 10. 1. 04:21
728x90
버튼을 누를때 알림이 오도록 하고 싶은데, 토큰관련과정과, 펑션과정과 INDEX.JS 와 FCM 서비스함수와 파이어베이스 백엔드 가 어떻게 연결되고 상호작용하는지 궁금해
 

ChatGPT의 말:

알림을 보내는 버튼을 눌렀을 때, 토큰 관련 과정, Firebase Functions (펑션) 과정, index.js (백엔드 코드), FCM 서비스 함수 그리고 Firebase 백엔드가 상호작용하는 전체 흐름을 설명하면 다음과 같습니다.

1. 토큰 관련 과정

  • **Firebase Cloud Messaging (FCM)**에서는 FCM 토큰을 사용하여 특정 기기로 푸시 알림을 전송합니다.
  • FCM 토큰은 앱이 설치되거나 실행될 때 Firebase Messaging을 통해 각 기기에 부여됩니다.
  • 앱이 실행되면, FirebaseMessaging.instance.getToken()을 통해 기기의 FCM 토큰을 받아 옵니다.
  • 이 토큰은 Firebase Firestore의 유저 정보에 저장하거나, Firebase Functions에서 사용할 수 있도록 백엔드로 전송합니다.

2. FCM 서비스 함수 과정

  • Flutter 앱에서 푸시 알림을 전송하는 기능을 구현할 때, PushNotificationService라는 클래스를 만들어 Firebase Cloud Functions에 푸시 알림을 요청합니다.
  • 버튼을 누르면 앱은 FCM 서비스 함수를 호출하여 푸시 알림을 전송할 데이터를 준비합니다. 이 데이터는 보통 푸시 알림의 제목, 내용, 그리고 알림을 받을 대상의 FCM 토큰을 포함합니다.

Flutter에서 FCM 서비스 함수 예시:

dart
코드 복사
class PushNotificationService { Future<void> sendNotification(String title, String body, String token) async { try { HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('sendPushNotification'); final result = await callable.call({ 'title': title, 'body': body, 'token': token, }); print("Notification sent: ${result.data}"); } catch (error) { print("Failed to send push notification: $error"); } } }
  • 이 함수는 Firebase Functions에 HTTP 요청을 보냅니다. 이때 Cloud Function이 FCM 푸시 알림을 처리하도록 구성되어 있습니다.

3. 펑션 과정 (Firebase Functions)

  • Firebase Functions서버리스 환경에서 동작하는 백엔드입니다. 여기서 index.js 파일을 작성하여, 앱이 보낸 푸시 알림 요청을 처리합니다.
  • 위에서 작성한 FCM 서비스 함수가 버튼 클릭 시 호출되면, 이 함수는 Firebase Functions로 해당 데이터를 전송합니다.

index.js 예시 (Cloud Functions 코드):

javascript
코드 복사
const functions = require("firebase-functions"); const admin = require("firebase-admin"); admin.initializeApp(); exports.sendPushNotification = functions.https.onCall(async (data, context) => { const { token, title, body } = data; const message = { notification: { title: title, body: body, }, token: token, }; try { const response = await admin.messaging().send(message); return { success: true, message: "Notification sent successfully" }; } catch (error) { console.error("Error sending message:", error); throw new functions.https.HttpsError("internal", "Error sending push notification"); } });
  • Firebase Functions는 데이터를 받아 admin.messaging().send(message) 메서드를 통해 Firebase Admin SDK를 사용해 Firebase FCM 서버에 푸시 알림을 요청합니다.

4. Firebase 백엔드

  • Firebase Admin SDKFCM 서버와 통신하는 역할을 합니다.
  • FCM 서버Firebase Admin SDK로부터 받은 요청을 처리하고, 특정 FCM 토큰을 통해 해당 기기로 푸시 알림을 전송합니다.

5. 전체 과정의 흐름

  1. 사용자(운영자)가 버튼을 누름
  2. Flutter 앱에서 FCM 서비스 함수 호출
  3. FCM 서비스 함수가 Firebase Functions 호출
  4. Firebase Functions가 Firebase Admin SDK를 사용해 푸시 알림을 요청
  5. Firebase FCM 서버가 해당 토큰을 가진 기기에 푸시 알림 전송
  6. 사용자(또는 모든 사용자)가 푸시 알림을 수신

요약

  1. FCM 토큰은 각 기기별로 생성되고 Firestore에 저장됩니다.
  2. 버튼을 눌렀을 때 Flutter 앱에서 Firebase Functions를 호출하여 푸시 알림 요청을 보냅니다.
  3. **Firebase Functions (index.js)**에서 FCM을 사용하여 알림을 전송합니다.
  4. Firebase FCM 서버FCM 토큰을 사용해 기기에 푸시 알림을 보냅니다.

이렇게 버튼 클릭 시 FCM 토큰, Cloud Functions, index.js 파일, Firebase Admin SDK, Firebase 백엔드가 상호작용하여 푸시 알림이 전송됩니다.

728x90

'♠♠기술 구현' 카테고리의 다른 글

페이스북 로그인 과정  (2) 2024.10.05
푸쉬알림 구현과정  (1) 2024.10.01
fcm 기본개념 다시 잡기  (0) 2024.10.01
fcm 구독으로 방향 바꾸기  (1) 2024.09.30
functions 배포  (0) 2024.09.28