기타

[FCM] Firebase Cloud Messaging?

Dev.Congsik 2024. 9. 10. 17:53
728x90

Firebase Cloud Messaging(FCM)은 Google의 Firebase 플랫폼의 일부로, Android 및 iOS 애플리케이션에 푸시 알림을 전송할 수 있는 서비스입니다. FCM을 사용하면 개발자는 클라이언트 애플리케이션에 메시지를 전송하여 사용자와의 상호작용을 강화할 수 있습니다.

1. FCM?

Firebase Cloud Messaging(FCM)은 클라우드에서 모바일 기기로 메시지를 전송할 수 있는 서비스입니다. 이를 통해 개발자는 사용자에게 실시간으로 알림을 보낼 수 있으며, 이는 앱의 활성화를 유지하는 데 도움이 됩니다.

 

2. FCM 기능

  • 알림 메시지: 사용자에게 푸시 알림을 보낼 수 있습니다. 예를 들어, 새로운 콘텐츠가 추가되었을 때 사용자에게 알림을 보낼 수 있습니다.
  • 데이터 메시지: 앱이 백그라운드에서 실행 중일 때 데이터를 전송할 수 있습니다. 이를 통해 앱의 상태를 업데이트하거나 특정 작업을 수행할 수 있습니다.
  • 주제 메시징: 특정 주제에 가입한 모든 사용자에게 메시지를 보낼 수 있습니다.
  • 조건 메시징: 특정 조건을 만족하는 사용자에게 메시지를 보낼 수 있습니다.
  • 기기 그룹 메시징: 특정 기기 그룹에 메시지를 보낼 수 있습니다.

 

3. FCM 원리

FCM은 클라이언트 애플리케이션과 FCM 서버 간의 통신을 통해 작동합니다. 기본적인 원리는 다음과 같습니다:

  1. 앱 등록: 애플리케이션이 FCM에 등록되어야 합니다. 이를 통해 앱은 고유한 등록 토큰을 받습니다.
  2. 메시지 전송: 서버는 FCM 서버에 메시지를 전송합니다. 메시지에는 대상 기기의 등록 토큰이 포함됩니다.
  3. 메시지 전달: FCM 서버는 메시지를 해당 기기로 전달합니다.
  4. 메시지 수신: 클라이언트 애플리케이션은 메시지를 수신하고, 이를 처리하여 사용자에게 알림을 표시하거나 다른 작업을 수행합니다.

 

4. FCM 구현

FCM을 Android 애플리케이션에 구현하는 단계는 다음과 같습니다:

 

4.1. Firebase 프로젝트 설정

  1. Firebase 콘솔에 로그인하고 새 프로젝트를 생성합니다.
  2. 프로젝트 설정에서 Android 앱 추가를 선택하고 패키지 이름을 입력합니다.
  3. google-services.json 파일을 다운로드하여 Android 프로젝트의 app 디렉토리에 추가합니다.

 

4.2. Android 프로젝트 설정

  1. build.gradle 파일을 수정하여 Firebase 및 FCM 종속성을 추가합니다.

프로젝트 build.gradle:

buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.10'
    }
}

앱 build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

dependencies {
    implementation 'com.google.firebase:firebase-messaging:23.0.0'
}

4.3. FCM 서비스 구현

  1. FirebaseMessagingService를 확장하여 메시지를 수신할 수 있는 서비스를 구현해 보겠습니다.
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // 메시지 수신 시 처리할 로직 작성
        if (remoteMessage.getNotification() != null) {
            // 알림 메시지 처리
            String title = remoteMessage.getNotification().getTitle();
            String body = remoteMessage.getNotification().getBody();
            sendNotification(title, body);
        }

        if (remoteMessage.getData().size() > 0) {
            // 데이터 메시지 처리
            Map<String, String> data = remoteMessage.getData();
            handleDataMessage(data);
        }
    }

    private void sendNotification(String title, String body) {
        // 알림 생성 및 표시 로직
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(title)
                .setContentText(body)
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }

    private void handleDataMessage(Map<String, String> data) {
        // 데이터 메시지 처리 로직
    }
}

 

728x90