feat: fcm 데이터 수신 방식 수정

- data-only 메시지만 수신 방식에서 notification + data로 수신 방식 변경
This commit is contained in:
klaus 2025-05-08 19:42:13 +09:00
parent db4bd56df2
commit 60190e099a
2 changed files with 38 additions and 11 deletions

View File

@ -5,6 +5,9 @@ import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.appsflyer.AppsFlyerLib import com.appsflyer.AppsFlyerLib
import com.appsflyer.deeplink.DeepLinkResult import com.appsflyer.deeplink.DeepLinkResult
import com.facebook.FacebookSdk import com.facebook.FacebookSdk
@ -18,9 +21,10 @@ import kr.co.vividnext.sodalive.di.AppDI
import kr.co.vividnext.sodalive.tracking.FirebaseTracking import kr.co.vividnext.sodalive.tracking.FirebaseTracking
import tech.notifly.Notifly import tech.notifly.Notifly
class SodaLiveApp : Application() { class SodaLiveApp : Application(), DefaultLifecycleObserver {
override fun onCreate() { override fun onCreate() {
super.onCreate() super<Application>.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
Logger.addLogAdapter(object : AndroidLogAdapter() { Logger.addLogAdapter(object : AndroidLogAdapter() {
override fun isLoggable(priority: Int, tag: String?): Boolean { override fun isLoggable(priority: Int, tag: String?): Boolean {
@ -58,8 +62,7 @@ class SodaLiveApp : Application() {
packageManager.getApplicationInfo(packageName, 0) packageManager.getApplicationInfo(packageName, 0)
} }
debuggable = 0 != appInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE debuggable = 0 != appInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
} catch (e: PackageManager.NameNotFoundException) { } catch (_: PackageManager.NameNotFoundException) {
/* debuggable variable will remain false */
} }
return debuggable return debuggable
@ -123,4 +126,17 @@ class SodaLiveApp : Application() {
BuildConfig.NOTIFLY_PASSWORD, BuildConfig.NOTIFLY_PASSWORD,
) )
} }
override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
isAppInForeground = true
}
override fun onStop(owner: LifecycleOwner) {
isAppInForeground = false
}
companion object {
var isAppInForeground = false
}
} }

View File

@ -3,7 +3,6 @@ package kr.co.vividnext.sodalive.fcm
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context
import android.content.Intent import android.content.Intent
import android.media.RingtoneManager import android.media.RingtoneManager
import android.os.Build import android.os.Build
@ -12,6 +11,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage import com.google.firebase.messaging.RemoteMessage
import kr.co.vividnext.sodalive.R import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.app.SodaLiveApp
import kr.co.vividnext.sodalive.common.Constants import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.common.SharedPreferenceManager import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.splash.SplashActivity import kr.co.vividnext.sodalive.splash.SplashActivity
@ -25,8 +25,13 @@ class SodaFirebaseMessagingService : FirebaseMessagingService() {
val intent = Intent("ACTION_POINT_GRANTED") val intent = Intent("ACTION_POINT_GRANTED")
intent.putExtra("message", remoteMessage.data["message"]) intent.putExtra("message", remoteMessage.data["message"])
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
} else if (
SodaLiveApp.isAppInForeground &&
remoteMessage.notification != null
) {
sendNotification(remoteMessage.data, remoteMessage.notification)
} else if (remoteMessage.data["message"]?.isNotBlank() == true) { } else if (remoteMessage.data["message"]?.isNotBlank() == true) {
sendNotification(remoteMessage.data) sendNotification(remoteMessage.data, remoteMessage.notification)
} }
} }
} }
@ -37,11 +42,14 @@ class SodaFirebaseMessagingService : FirebaseMessagingService() {
SharedPreferenceManager.pushToken = token SharedPreferenceManager.pushToken = token
} }
private fun sendNotification(messageData: Map<String, String>) { private fun sendNotification(
messageData: Map<String, String>,
notification: RemoteMessage.Notification?
) {
val notificationChannelId = getString(R.string.default_notification_channel_id) val notificationChannelId = getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationManager = val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed. // Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -91,16 +99,19 @@ class SodaFirebaseMessagingService : FirebaseMessagingService() {
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
) )
val title = notification?.title ?: messageData["title"]
val message = notification?.body ?: messageData["message"]
val notificationBuilder = NotificationCompat.Builder(this, notificationChannelId) val notificationBuilder = NotificationCompat.Builder(this, notificationChannelId)
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageData["title"]) .setContentTitle(title)
.setContentText(messageData["message"]) .setContentText(message)
.setSound(defaultSoundUri) .setSound(defaultSoundUri)
.setAutoCancel(true) .setAutoCancel(true)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
val bigTextStyle = NotificationCompat.BigTextStyle(notificationBuilder) val bigTextStyle = NotificationCompat.BigTextStyle(notificationBuilder)
bigTextStyle.bigText(messageData["message"]) bigTextStyle.bigText(message)
notificationManager.notify(System.currentTimeMillis().toInt(), notificationBuilder.build()) notificationManager.notify(System.currentTimeMillis().toInt(), notificationBuilder.build())
} }