Skip to content

Commit

Permalink
[FIX] 알림 클릭 1회만 되는 현상 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kim0hoon committed Oct 30, 2023
1 parent 3f6abba commit fe37d87
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class PolzzakFirebaseMessagingService : FirebaseMessagingService() {

private fun notify(title: String, content: String) {
val notificationManager = NotificationManagerCompat.from(applicationContext)
val intent = Intent(applicationContext, MainActivity::class.java)
val intent = Intent(applicationContext, MainActivity::class.java).apply {
putExtra("requestCode", PENDING_INTENT_REQUEST_CODE)
}
val pendingIntent = PendingIntent.getActivity(
applicationContext,
PENDING_INTENT_REQUEST_CODE,
Expand Down Expand Up @@ -78,9 +80,9 @@ class PolzzakFirebaseMessagingService : FirebaseMessagingService() {
private const val NOTIFICATION_CHANNEL_NAME = "push alarm"
private const val NOTIFICATION_CHANNEL_ID = "push_notification_id"
private const val NOTIFICATION_ID = 1
private const val PENDING_INTENT_REQUEST_CODE = 1000
private const val NOTIFICATION_TITLE_KEY = "title"
private const val NOTIFICATION_BODY_KEY = "body"
const val PENDING_INTENT_REQUEST_CODE = 1000
var receiveMessage: Boolean = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ class NotificationListViewModel @AssistedInject constructor(
private val notificationHorizontalScrollPositionMap = HashMap<Int, Int>()
private val notificationMutex = Mutex()

init {
initNotifications()
}

fun initNotifications() {
val priority = INIT_NOTIFICATIONS_PRIORITY
if (requestNotificationJobData.getPriorityOrZero() < priority) requestNotificationJobData?.job?.cancel()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.polzzak_android.presentation.feature.notification.list.base

import android.os.Bundle
import androidx.annotation.IdRes
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -30,6 +32,7 @@ import com.polzzak_android.presentation.feature.notification.list.item.Notificat
import com.polzzak_android.presentation.feature.notification.list.item.NotificationItem
import com.polzzak_android.presentation.feature.notification.list.item.NotificationSkeletonLoadingItem
import com.polzzak_android.presentation.feature.notification.list.model.NotificationModel
import com.polzzak_android.presentation.feature.root.MainViewModel
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

Expand All @@ -39,6 +42,8 @@ abstract class BaseNotificationListFragment : BaseFragment<FragmentNotificationL
NotificationListClickListener {
override val layoutResId: Int = R.layout.fragment_notification_list

private val mainViewModel by activityViewModels<MainViewModel>()

@Inject
lateinit var notificationListViewModelAssistedFactory: NotificationListViewModel.NotificationAssistedFactory
private val notificationListViewModel by viewModels<NotificationListViewModel>(factoryProducer = {
Expand All @@ -52,8 +57,8 @@ abstract class BaseNotificationListFragment : BaseFragment<FragmentNotificationL
abstract val actionToSettingFragment: Int
abstract val memberType: MemberType

override fun onResume() {
super.onResume()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
notificationListViewModel.initNotifications()
}

Expand Down Expand Up @@ -120,6 +125,7 @@ abstract class BaseNotificationListFragment : BaseFragment<FragmentNotificationL
super.initObserver()
initNotificationObserver()
initErrorEventObserver()
initNotificationMessageObserver()
}

private fun initNotificationObserver() {
Expand Down Expand Up @@ -168,6 +174,14 @@ abstract class BaseNotificationListFragment : BaseFragment<FragmentNotificationL
})
}

private fun initNotificationMessageObserver() {
mainViewModel.refreshNotificationLiveEvent.observe(
viewLifecycleOwner,
EventWrapperObserver {
notificationListViewModel.initNotifications()
})
}

private fun createSkeletonLoadingItems() = List(LOADING_SKELETON_ITEM_COUNT) {
NotificationSkeletonLoadingItem()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.polzzak_android.databinding.ActivityMainBinding
import com.polzzak_android.presentation.common.base.BaseActivity
import com.polzzak_android.presentation.common.model.ButtonCount
import com.polzzak_android.presentation.common.model.CommonButtonModel
import com.polzzak_android.presentation.common.service.PolzzakFirebaseMessagingService
import com.polzzak_android.presentation.common.util.InAppUpdateChecker
import com.polzzak_android.presentation.common.util.PermissionManager
import com.polzzak_android.presentation.common.util.SpannableBuilder
Expand Down Expand Up @@ -90,7 +91,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), SocialLoginManager {

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
supportFragmentManager.setFragmentResult(NOTIFICATION_INTENT_REQUEST_KEY, Bundle())
when (intent?.extras?.getInt("requestCode")) {
PolzzakFirebaseMessagingService.PENDING_INTENT_REQUEST_CODE -> mainViewModel.clickNotificationMessage()
else -> {
//do nothing
}
}
}

override fun onSaveInstanceState(outState: Bundle) {
Expand Down Expand Up @@ -157,6 +163,5 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), SocialLoginManager {
companion object {
private const val BACK_BTN_DEBOUNCE_TIMER = 3000
private const val SAVE_INSTANCE_ACCESS_TOKEN_KEY = "save_instance_access_token_key"
const val NOTIFICATION_INTENT_REQUEST_KEY = "notification_intent_request_key"
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package com.polzzak_android.presentation.feature.root

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.polzzak_android.common.util.livedata.EventWrapper
import com.polzzak_android.presentation.common.service.PolzzakFirebaseMessagingService

class MainViewModel : ViewModel() {
var accessToken: String? = null
private set

private val _moveNotificationTabLiveEvent = MutableLiveData<EventWrapper<Boolean>>()
val moveNotificationTabLiveEvent: LiveData<EventWrapper<Boolean>> =
_moveNotificationTabLiveEvent

private val _refreshNotificationLiveEvent = MutableLiveData<EventWrapper<Boolean>>()
val refreshNotificationLiveEvent: LiveData<EventWrapper<Boolean>> =
_refreshNotificationLiveEvent

fun logout() {
accessToken = null
PolzzakFirebaseMessagingService.receiveMessage = false
Expand All @@ -16,4 +27,12 @@ class MainViewModel : ViewModel() {
this.accessToken = accessToken
PolzzakFirebaseMessagingService.receiveMessage = true
}

fun clickNotificationMessage() {
_moveNotificationTabLiveEvent.value = EventWrapper(true)
}

fun refreshNotifications() {
_refreshNotificationLiveEvent.value = EventWrapper(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import androidx.activity.OnBackPressedCallback
import androidx.core.content.ContextCompat
import androidx.core.view.MenuItemCompat
import androidx.core.view.children
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import com.polzzak_android.R
import com.polzzak_android.common.util.livedata.EventWrapperObserver
import com.polzzak_android.databinding.FragmentKidHostBinding
import com.polzzak_android.presentation.common.base.BaseFragment
import com.polzzak_android.presentation.feature.root.MainActivity
import com.polzzak_android.presentation.feature.root.MainViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -25,6 +27,8 @@ class KidHostFragment : BaseFragment<FragmentKidHostBinding>() {

private val hostViewModel by viewModels<HostViewModel>()

private val mainViewModel by activityViewModels<MainViewModel>()

// 시스템 back 버튼 동작 가로치개 위한 callback
private val backPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Expand All @@ -41,7 +45,6 @@ class KidHostFragment : BaseFragment<FragmentKidHostBinding>() {
activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner, backPressedCallback)

setupNavigationView()
initFragmentResultListener()
}

private fun setupNavigationView() {
Expand Down Expand Up @@ -88,14 +91,12 @@ class KidHostFragment : BaseFragment<FragmentKidHostBinding>() {
}
}
}
}

private fun initFragmentResultListener() {
activity?.supportFragmentManager?.setFragmentResultListener(
MainActivity.NOTIFICATION_INTENT_REQUEST_KEY,
viewLifecycleOwner
) { _, _ ->
binding.kidBtmNav.selectedItemId = R.id.kid_notification_nav_graph
}
mainViewModel.moveNotificationTabLiveEvent.observe(
viewLifecycleOwner,
EventWrapperObserver {
if (binding.kidBtmNav.selectedItemId == R.id.kid_notification_nav_graph) mainViewModel.refreshNotifications()
else binding.kidBtmNav.selectedItemId = R.id.kid_notification_nav_graph
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import androidx.activity.OnBackPressedCallback
import androidx.core.content.ContextCompat
import androidx.core.view.MenuItemCompat
import androidx.core.view.children
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.polzzak_android.R
import com.polzzak_android.common.util.livedata.EventWrapperObserver
import com.polzzak_android.databinding.FragmentProtectorHostBinding
import com.polzzak_android.presentation.common.base.BaseFragment
import com.polzzak_android.presentation.feature.root.MainActivity
import com.polzzak_android.presentation.feature.root.MainViewModel

class ProtectorHostFragment() : BaseFragment<FragmentProtectorHostBinding>() {

Expand All @@ -22,12 +24,13 @@ class ProtectorHostFragment() : BaseFragment<FragmentProtectorHostBinding>() {

private val hostViewModel by viewModels<HostViewModel>()

private val mainViewModel by activityViewModels<MainViewModel>()

override fun initView() {
super.initView()

setupNavigationView()
activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner, backPressedCallback)
initFragmentResultListener()
}

override fun onResume() {
Expand Down Expand Up @@ -89,14 +92,12 @@ class ProtectorHostFragment() : BaseFragment<FragmentProtectorHostBinding>() {
}
}
}
}

private fun initFragmentResultListener() {
activity?.supportFragmentManager?.setFragmentResultListener(
MainActivity.NOTIFICATION_INTENT_REQUEST_KEY,
viewLifecycleOwner
) { _, _ ->
binding.protectorBtmNav.selectedItemId = R.id.protectorNotificationFragment
}
mainViewModel.moveNotificationTabLiveEvent.observe(
viewLifecycleOwner,
EventWrapperObserver {
if (binding.protectorBtmNav.selectedItemId == R.id.protectorNotificationFragment) mainViewModel.refreshNotifications()
else binding.protectorBtmNav.selectedItemId = R.id.protectorNotificationFragment
})
}
}

0 comments on commit fe37d87

Please sign in to comment.