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.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.appsflyer.AppsFlyerLib
import com.appsflyer.deeplink.DeepLinkResult
import com.facebook.FacebookSdk
@ -18,9 +21,10 @@ import kr.co.vividnext.sodalive.di.AppDI
import kr.co.vividnext.sodalive.tracking.FirebaseTracking
import tech.notifly.Notifly
class SodaLiveApp : Application() {
class SodaLiveApp : Application(), DefaultLifecycleObserver {
override fun onCreate() {
super.onCreate()
super<Application>.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
Logger.addLogAdapter(object : AndroidLogAdapter() {
override fun isLoggable(priority: Int, tag: String?): Boolean {
@ -58,8 +62,7 @@ class SodaLiveApp : Application() {
packageManager.getApplicationInfo(packageName, 0)
}
debuggable = 0 != appInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
} catch (e: PackageManager.NameNotFoundException) {
/* debuggable variable will remain false */
} catch (_: PackageManager.NameNotFoundException) {
}
return debuggable
@ -123,4 +126,17 @@ class SodaLiveApp : Application() {
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.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import android.os.Build
@ -12,6 +11,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
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.SharedPreferenceManager
import kr.co.vividnext.sodalive.splash.SplashActivity
@ -25,8 +25,13 @@ class SodaFirebaseMessagingService : FirebaseMessagingService() {
val intent = Intent("ACTION_POINT_GRANTED")
intent.putExtra("message", remoteMessage.data["message"])
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
} else if (
SodaLiveApp.isAppInForeground &&
remoteMessage.notification != null
) {
sendNotification(remoteMessage.data, remoteMessage.notification)
} 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
}
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 defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -91,16 +99,19 @@ class SodaFirebaseMessagingService : FirebaseMessagingService() {
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)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageData["title"])
.setContentText(messageData["message"])
.setContentTitle(title)
.setContentText(message)
.setSound(defaultSoundUri)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
val bigTextStyle = NotificationCompat.BigTextStyle(notificationBuilder)
bigTextStyle.bigText(messageData["message"])
bigTextStyle.bigText(message)
notificationManager.notify(System.currentTimeMillis().toInt(), notificationBuilder.build())
}