notepad by Oxix
FCM 메시지 처리 본문
메시지를 받으려면 FirebaseMessagingService를 확장하는 서비스를 사용해야 한다. FirebaseMessagingService를 사용하려면 매니페스트에 다음을 추가해야 한다.
또한 알림 모양을 지정하려면 기본 값을 설정하는 것이 좋다. application 태그 안에 다음 줄을 추가하여 사용자 정의 기본 아이콘과 색상을 설정할 수 있다.
onMessageReceived
FirebaseMessagingService.onMessageReceived 메서드를 재정의하면 수신된 RemoteMessage 객체를 기반으로 작업을 수행하고 메시지 데이터를 가져올 수 있다.
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
Log.d(TAG, "From: ${remoteMessage.from}")
// Check if message contains a data payload.
if (remoteMessage.data.isNotEmpty()) {
Log.d(TAG, "Message data payload: ${remoteMessage.data}")
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use WorkManager.
scheduleJob()
} else {
// Handle message within 10 seconds
handleNow()
}
}
// Check if message contains a notification payload.
remoteMessage.notification?.let {
Log.d(TAG, "Message Notification Body: ${it.body}")
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
Firebase 알림 수신 앱의 포그라운드/백그라운드 상태에 따라 다르게 동작한다. 포그라운드 앱이 알림 메시지 또는 데이터 메시지를 수신하도록 하려면 onMessageReceived 콜백을 처리하는 코드를 작성해야 한다.
onMessageReceived는 다음을 제외하고 대부분의 메시지 유형에 제공된다.
-
앱이 백그라운드에 있을 때 알림 메시지가 전달된다 . 이 경우 알림은 장치의 시스템 트레이로 전달된다. 사용자가 알림을 탭하면 기본적으로 앱 시작 관리자가 열린다.
-
백그라운드에서 수신될 때 알림 및 데이터 페이로드가 모두 포함된 메시지 . 이 경우 알림은 장치의 시스템 트레이에 전달되고 데이터 페이로드는 런처 활동의 추가 의도로 전달된다.
백그라운드 앱에서 알림 메시지 처리
백그라운드에 있는 경우 Android는 알림 메시지를 시스템 트레이로 보낸다. 사용자가 알림을 탭하는 경우 기본적으로 앱 런처가 열린다. 백그라운드 활동을 제한된 경우 메시지를 전달하지 않을 수 있다.
'Android' 카테고리의 다른 글
RemoteConfig (0) | 2022.12.26 |
---|---|
FCM Android 설정 (0) | 2022.12.21 |
FCM 유형 (0) | 2022.12.21 |
좋은아키텍처를 위해 - 복잡성 제거 (0) | 2022.12.08 |