11package com.texthip.thip
22
3+ import android.content.Intent
34import android.os.Bundle
5+ import android.util.Log
46import androidx.activity.ComponentActivity
57import androidx.activity.compose.setContent
68import androidx.activity.enableEdgeToEdge
79import androidx.activity.result.contract.ActivityResultContracts
810import androidx.compose.runtime.Composable
911import androidx.compose.runtime.LaunchedEffect
12+ import androidx.compose.runtime.getValue
13+ import androidx.compose.runtime.mutableStateOf
14+ import androidx.compose.runtime.setValue
1015import androidx.navigation.compose.NavHost
1116import androidx.navigation.compose.composable
1217import androidx.navigation.compose.rememberNavController
@@ -32,21 +37,96 @@ class MainActivity : ComponentActivity() {
3237 ActivityResultContracts .RequestPermission ()
3338 ) {}
3439
40+ private var notificationData by mutableStateOf<NotificationData ?>(null )
41+
42+ data class NotificationData (
43+ val notificationId : String? ,
44+ val fromNotification : Boolean
45+ )
46+
3547 override fun onCreate (savedInstanceState : Bundle ? ) {
3648 super .onCreate(savedInstanceState)
3749 enableEdgeToEdge()
38-
50+
3951 // 앱 시작 시 알림 권한 요청
4052 requestNotificationPermissionIfNeeded()
41-
53+
54+ // 푸시 알림에서 온 데이터 처리
55+ handleNotificationIntent(intent)
56+
4257 setContent {
4358 ThipTheme {
44- RootNavHost (authStateManager)
59+ RootNavHost (
60+ authStateManager = authStateManager,
61+ notificationData = notificationData
62+ )
4563 }
4664 }
4765// getKakaoKeyHash(this)
4866 }
4967
68+ override fun onNewIntent (intent : Intent ) {
69+ super .onNewIntent(intent)
70+
71+ // 새로운 Intent가 들어올 때 (백그라운드에서 알림 클릭 시)
72+ handleNotificationIntent(intent)
73+ }
74+
75+ private fun handleNotificationIntent (intent : Intent ) {
76+ Log .d(" MainActivity" , " Handling notification intent with extras: ${intent.extras?.keySet()} " )
77+
78+ val customNotificationId = intent.getStringExtra(" notification_id" )
79+ val customFromNotification = intent.getBooleanExtra(" from_notification" , false )
80+
81+ // FCM 백그라운드 알림에서 온 데이터 확인 (시스템이 자동 생성한 알림의 경우)
82+ val fcmNotificationId = intent.getStringExtra(" gcm.notification.data.notificationId" )
83+ ? : intent.getStringExtra(" notificationId" )
84+
85+ var newNotificationData: NotificationData ? = null
86+
87+ // 커스텀 알림에서 온 경우 (포그라운드에서 생성된 알림)
88+ if (customFromNotification && customNotificationId != null ) {
89+ Log .d(" MainActivity" , " Processing custom notification: $customNotificationId " )
90+ newNotificationData = NotificationData (customNotificationId, customFromNotification)
91+
92+ // Intent extras 완전 제거
93+ cleanupNotificationExtras(intent, listOf (" notification_id" , " from_notification" ))
94+ }
95+ // FCM 백그라운드 시스템 알림에서 온 경우
96+ else if (fcmNotificationId != null ) {
97+ Log .d(" MainActivity" , " Processing FCM notification: $fcmNotificationId " )
98+ newNotificationData = NotificationData (fcmNotificationId, true )
99+
100+ // Intent extras 완전 제거
101+ cleanupNotificationExtras(intent, listOf (
102+ " gcm.notification.data.notificationId" ,
103+ " notificationId"
104+ ))
105+ }
106+
107+ // 새로운 알림 데이터가 있고, 기존 데이터와 다른 경우에만 업데이트
108+ if (newNotificationData != null && newNotificationData != notificationData) {
109+ Log .d(" MainActivity" , " Setting new notification data: ${newNotificationData.notificationId} " )
110+ notificationData = newNotificationData
111+ } else if (newNotificationData != null ) {
112+ Log .d(" MainActivity" , " Notification data unchanged, skipping update" )
113+ }
114+ }
115+
116+ private fun cleanupNotificationExtras (intent : Intent , keys : List <String >) {
117+ keys.forEach { key ->
118+ try {
119+ intent.removeExtra(key)
120+ Log .v(" MainActivity" , " Removed extra: $key " )
121+ } catch (e: Exception ) {
122+ Log .w(" MainActivity" , " Failed to remove extra: $key " , e)
123+ }
124+ }
125+
126+ // Intent 플래그도 정리
127+ intent.replaceExtras(intent.extras)
128+ }
129+
50130 private fun requestNotificationPermissionIfNeeded () {
51131 if (NotificationPermissionUtils .shouldRequestNotificationPermission(this )) {
52132 notificationPermissionLauncher.launch(android.Manifest .permission.POST_NOTIFICATIONS )
@@ -55,7 +135,10 @@ class MainActivity : ComponentActivity() {
55135}
56136
57137@Composable
58- fun RootNavHost (authStateManager : AuthStateManager ) {
138+ fun RootNavHost (
139+ authStateManager : AuthStateManager ,
140+ notificationData : MainActivity .NotificationData ? = null
141+ ) {
59142 val navController = rememberNavController()
60143
61144 LaunchedEffect (Unit ) {
@@ -66,6 +149,7 @@ fun RootNavHost(authStateManager: AuthStateManager) {
66149 }
67150 }
68151
152+
69153 NavHost (
70154 navController = navController,
71155 startDestination = CommonRoutes .Splash
@@ -104,7 +188,8 @@ fun RootNavHost(authStateManager: AuthStateManager) {
104188 inclusive = true
105189 }
106190 }
107- }
191+ },
192+ notificationData = notificationData
108193 )
109194 }
110195 }
0 commit comments