From 60190e099a6c0f0c3d6c2045980f77f8ebbddffb Mon Sep 17 00:00:00 2001 From: klaus Date: Thu, 8 May 2025 19:42:13 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20fcm=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EC=88=98=EC=8B=A0=20=EB=B0=A9=EC=8B=9D=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?-=20data-only=20=EB=A9=94=EC=8B=9C=EC=A7=80=EB=A7=8C=20?= =?UTF-8?q?=EC=88=98=EC=8B=A0=20=EB=B0=A9=EC=8B=9D=EC=97=90=EC=84=9C=20not?= =?UTF-8?q?ification=20+=20data=EB=A1=9C=20=EC=88=98=EC=8B=A0=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../co/vividnext/sodalive/app/SodaLiveApp.kt | 24 +++++++++++++++--- .../fcm/SodaFirebaseMessagingService.kt | 25 +++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/kr/co/vividnext/sodalive/app/SodaLiveApp.kt b/app/src/main/java/kr/co/vividnext/sodalive/app/SodaLiveApp.kt index e24290a..4b7ffab 100644 --- a/app/src/main/java/kr/co/vividnext/sodalive/app/SodaLiveApp.kt +++ b/app/src/main/java/kr/co/vividnext/sodalive/app/SodaLiveApp.kt @@ -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.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 + } } diff --git a/app/src/main/java/kr/co/vividnext/sodalive/fcm/SodaFirebaseMessagingService.kt b/app/src/main/java/kr/co/vividnext/sodalive/fcm/SodaFirebaseMessagingService.kt index ceaf17c..edb0bec 100644 --- a/app/src/main/java/kr/co/vividnext/sodalive/fcm/SodaFirebaseMessagingService.kt +++ b/app/src/main/java/kr/co/vividnext/sodalive/fcm/SodaFirebaseMessagingService.kt @@ -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) { + private fun sendNotification( + messageData: Map, + 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()) }