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()) }