fix(#214): notification tap now navigates to correct session

Root cause: MainActivity had no onNewIntent override. When the app was
backgrounded and a notification was tapped, Android brought the existing
task to the foreground and delivered the intent via onNewIntent — but
since it wasn't overridden, the session ID from the notification was
silently dropped and the user saw the last-active session.

Fix:
1. Changed notificationSessionId from a local val to a Compose
   mutableStateOf so it's observable across recompositions.
2. Added onNewIntent override that reads the session ID from the
   incoming intent and updates the observable state.
3. Added FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP to the
   notification PendingIntent for reliable task management.
This commit is contained in:
M57 committed 2026-06-21 16:40:30 +03:00
1 parent 262578528b
commit d2e59b2741
2 files changed
+20 -4

No files matched your search

@@ -1,5 +1,6 @@
package com.m57.hermescontrol
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
@@ -9,17 +10,21 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import com.m57.hermescontrol.data.local.AuthManager
import com.m57.hermescontrol.notification.NotificationReplyReceiver
import com.m57.hermescontrol.theme.HermesControlTheme
class MainActivity : ComponentActivity() {
// Observable state so both onCreate and onNewIntent updates flow to Compose
private var notificationSessionId by mutableStateOf<String?>(null)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Read notification tap sessionId (if any) from the intent that launched us
val notificationSessionId = intent?.getStringExtra(NotificationReplyReceiver.EXTRA_SESSION_ID)
notificationSessionId = intent?.getStringExtra(NotificationReplyReceiver.EXTRA_SESSION_ID)
enableEdgeToEdge()
setContent {
@@ -40,4 +45,13 @@ class MainActivity : ComponentActivity() {
}
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
// When a notification tap arrives while the app is already running
// (task exists in background), Android delivers the intent here instead
// of onCreate. Update the observable state so Compose recomposes
// and ChatScreen's LaunchedEffect switches to the correct session.
notificationSessionId = intent.getStringExtra(NotificationReplyReceiver.EXTRA_SESSION_ID)
}
}
@@ -153,9 +153,11 @@ class ChatNotificationService : Service() {
}
private fun buildContentIntent(sessionId: String?): PendingIntent {
val intent = Intent(this, MainActivity::class.java)
if (!sessionId.isNullOrBlank()) {
intent.putExtra(NotificationReplyReceiver.EXTRA_SESSION_ID, sessionId)
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
if (!sessionId.isNullOrBlank()) {
putExtra(NotificationReplyReceiver.EXTRA_SESSION_ID, sessionId)
}
}
return PendingIntent.getActivity(
this,