IT/Android

[Android/FCM] (4) Android 설정

Hodie! 2023. 3. 7. 15:55
반응형

내용

본격적으로 안드로이드에서 FCM을 어떻게 적용하는 지에 대해 알아보고자 한다.

SDK 설정

기본적으로 다른 Firebase 기능이 추가한 상태인 경우 어느정도 설정이 되어있을 것이다.

추가적으로 FCM을 위해 추가해야하는 것은 app수준의 gradle 파일에 추가하는 것 정도이다.

dependencies {
    // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:31.2.0')

    // Add the dependencies for the Firebase Cloud Messaging and Analytics libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
}

App Manifest 수정

기본 설정

  • FirebaseMessagingService 를 상속하는 서비스 추가
    • Background에서 앱에 대한 알림을 수신하는 것 이상의 메시지 핸들링을 하려는 경우에 필요하다.
    • Foreground에서 알림을 수신하고, data payload를 수신하고, upstream 메시지를 전송하는 등의 작업을 수행하려면 이 service를 extend해야 한다.
    • <service
      android:name=".java.MyFirebaseMessagingService"
      android:exported="false">
      <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
      </service>
    • name에는 FirebaseMessagingService를 상속받은 service class를 생성한뒤 파일의 경로를 포함해 작성해주면 된다.service 파일을 override함으로써 notification을 구현하면 된다.
  • (선택사항) 추가적으로, Application Component 내에서 메타데이터 요소는 default notifcation 아이콘 및 색상을 지정한다. 안드로이드수신 메시지가 아이콘이나 색상을 명시적으로 설정하지 않을 경우, 해당 값을 사용하게 된다.
    • <!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
      See README(https://goo.gl/l4GJaQ) for more. -->
      <meta-data
      android:name="com.google.firebase.messaging.default_notification_icon"
      android:resource="@drawable/ic_stat_ic_notification" />
      <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
      notification message. See README(https://goo.gl/6BKBk7) for more. -->
      <meta-data
      android:name="com.google.firebase.messaging.default_notification_color"
      android:resource="@color/colorAccent" />
  • (선택사항) Android 8.0(API Level 26)부터, Notification Channel이 지원 및 권장된다.
    • FCM은 기본 설정으로 default notification channel을 제공한다. 만약, 고유한 default channeld을 만들고 사용하기를 원한다면, default_notification_channel_id를 아래처럼 Notification Channel object의 ID로 설정하면 된다.
    • FCM은 들어오는 메시지가 notification channel명시적으로 설정하지 않을 때마다, 해당 값을 사용하게 된다. 더 자세한 내용은 Manage notification channels를 참고하면 된다.
    • <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="@string/default_notification_channel_id" />

Auto Initialization 방지

FCM Registration token이 생성되면, 라이브러리가 identifierconfiguration dataFirebase로 자동으로 업로드 한다.

필요성

문제는, 자체 앱 서버에 유저의 FCM Registration Token을 저장하고 이를 이용해서 알림을 주고받는데 기기에서 자동으로 만들어지는 FCM Token과 서버가 가지고 있는 해당 유저에 대한 FCM Token이 달라지면 올바른 알림을 보내줄 수 없게된다. 그렇기 때문에 이를 방지하기 위해 Auto-Initialization을 방지하는 듯 하다.

방법

Token Auto-generation을 방지하려면, 다음 메타데이터 값을 AndroidManifest.xml에 추가해, Analytics 수집 및 FCM 자동 초기화를 비활성화 한다. (둘 다 비활성화 해야함)

<meta-data
    android:name="firebase_messaging_auto_init_enabled"
    android:value="false" />
<meta-data
    android:name="firebase_analytics_collection_enabled"
    android:value="false" />

FCM auto-init을 다시 enable 처리하려면 원하는 곳에 아래와 같이 runtime 호출을 수행하면 된다.

Firebase.messaging.isAutoInitEnabled = true

 

Analytics 수집을 다시 활성화하려면, FirebaseAnalyticssetAnalyticsCollectionEnabled() 메소드를 호출한다. 이 값은 일단 설정되면, 앱을 다시 시작해도 지속된다.

반응형