From 012f0c9dfc0dedbeca16e5741def787c473e28e0 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Tue, 17 Nov 2026 11:12:42 +1100 Subject: [PATCH] Notify messages stored before the notification handlers subscribe A push or background poll that cold-starts the process stores its message before NotificationProcessor subscribes to the database change flows, which keep no replay, so the message never notified. Since the notification refactor in 1.33.0 this has hit every message that wakes a killed process. Mark each message a handler deals with in the existing, unused notified column, and have each subscription start by replaying the unread incoming messages still unmarked as Added events. Messages a handler already saw aren't replayed, so a dismissed unread message doesn't notify again on each cold start. The mark is per row because sms ids are reused after its newest rows are deleted. --- .../FullAndNameOnlyNotificationHandler.kt | 20 +- .../notifications/MissedMessageEvents.kt | 135 ++++++++++++ .../NoNameOrContentNotificationHandler.kt | 20 +- .../notifications/MissedMessageEventsTest.kt | 194 ++++++++++++++++++ 4 files changed, 357 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/notifications/MissedMessageEvents.kt create mode 100644 app/src/test/java/org/thoughtcrime/securesms/notifications/MissedMessageEventsTest.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/FullAndNameOnlyNotificationHandler.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/FullAndNameOnlyNotificationHandler.kt index 67c291a182..bc971f1f08 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/FullAndNameOnlyNotificationHandler.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/FullAndNameOnlyNotificationHandler.kt @@ -10,7 +10,6 @@ import org.session.libsession.utilities.Phrase import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.FlowCollector import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.merge import network.loki.messenger.R import org.session.libsession.utilities.Address import org.session.libsession.utilities.StringSubstitutionConstants @@ -69,6 +68,7 @@ class FullAndNameOnlyNotificationHandler @Inject constructor( notificationManager: NotificationManagerCompat, prefs: PreferenceStorage, appVisibilityManager: AppVisibilityManager, + private val missedMessageEvents: MissedMessageEvents, ) : ThreadBasedNotificationHandler( context = context, currentActivityObserver = currentActivityObserver, @@ -90,15 +90,23 @@ class FullAndNameOnlyNotificationHandler @Inject constructor( suspend fun process() { - merge( - threadDb.changeNotification.map(::ThreadUpdated), - mmsDatabase.changeNotification.map(::MessageUpdated), - smsDatabase.changeNotification.map(::MessageUpdated), - reactionDatabase.changeNotification.map(::ReactionUpdated), + missedMessageEvents.withMissedMessages( + live = listOf( + threadDb.changeNotification.map(::ThreadUpdated), + mmsDatabase.changeNotification.map(::MessageUpdated), + smsDatabase.changeNotification.map(::MessageUpdated), + reactionDatabase.changeNotification.map(::ReactionUpdated), + ), + toEvent = ::MessageUpdated, ).collect(object : FlowCollector { private val lastPostedMessageTimestampByThreadId = MutableLongLongMap() override suspend fun emit(value: Event) { + handle(value) + if (value is MessageUpdated) missedMessageEvents.markSeen(value.change) + } + + private suspend fun handle(value: Event) { // Whether this event can only update an existing notification (i.e. no action // if there's no existing notification, and no loud notify on the updated contents) var updateOnly: Boolean diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/MissedMessageEvents.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/MissedMessageEvents.kt new file mode 100644 index 0000000000..9af79d7f51 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/MissedMessageEvents.kt @@ -0,0 +1,135 @@ +package org.thoughtcrime.securesms.notifications + +import androidx.sqlite.db.SupportSQLiteDatabase +import androidx.sqlite.db.SupportSQLiteOpenHelper +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.channelFlow +import kotlinx.coroutines.launch +import org.session.libsignal.utilities.Log +import org.thoughtcrime.securesms.database.MmsDatabase +import org.thoughtcrime.securesms.database.MmsSmsColumns +import org.thoughtcrime.securesms.database.SmsDatabase +import org.thoughtcrime.securesms.database.model.MessageChanges +import org.thoughtcrime.securesms.database.model.MessageId +import org.thoughtcrime.securesms.preferences.PreferenceKey +import org.thoughtcrime.securesms.preferences.PreferenceStorage +import javax.inject.Inject +import javax.inject.Provider +import javax.inject.Singleton + +/** + * Replays the incoming messages that no notification handler has seen. + * + * The handlers learn about new messages from the databases' change flows, which keep no replay. A + * push or background poll that cold-starts the process stores its message before + * [NotificationProcessor] subscribes, so the message's Added event is dropped and it never notifies. + * Each message a handler deals with is marked in the `notified` column, and each subscription starts + * by replaying the unread incoming messages still unmarked. Messages a handler already saw aren't + * replayed, so an unread message the user dismissed doesn't notify again on every cold start. + * + * The mark is per row rather than a high-water id because sms has no AUTOINCREMENT: deleting its + * newest rows frees their ids for the next inserts. + */ +@Singleton +class MissedMessageEvents @Inject constructor( + private val prefs: PreferenceStorage, + private val openHelper: Provider, +) { + /** + * The events from [live], plus one Added event for each thread with messages no handler has + * seen. [live] is subscribed before the database is read, so a message stored in between + * arrives at least once; the handlers already ignore a message they've notified. + */ + fun withMissedMessages(live: List>, toEvent: (MessageChanges) -> T): Flow = channelFlow { + for (flow in live) { + // Undispatched, so the change flow has this subscriber before the read below. + launch(start = CoroutineStart.UNDISPATCHED) { + flow.collect { send(it) } + } + } + + for (change in findMissed()) { + send(toEvent(change)) + } + } + + /** + * Records that a handler has dealt with [change], so it isn't replayed. Call it once the + * handler has finished with the event: a process that dies before then replays it next time. + */ + fun markSeen(change: MessageChanges) { + if (change.changeType != MessageChanges.ChangeType.Added) return + + val db = openHelper.get().writableDatabase + for ((isMms, ids) in change.ids.groupBy { it.mms }) { + db.execSQL(""" + UPDATE ${tableName(isMms)} SET ${MmsSmsColumns.NOTIFIED} = 1 + WHERE ${MmsSmsColumns.ID} IN (${ids.joinToString(",") { it.id.toString() }}) + AND ${MmsSmsColumns.NOTIFIED} = 0 + """) + } + } + + private fun findMissed(): List { + val db = openHelper.get().writableDatabase + + if (!prefs[TRACKING_STARTED]) { + // Nothing has been marked yet, e.g. the first run of this version. Mark what's stored + // now rather than notifying every unread message. + db.markAllUnreadNotified() + prefs[TRACKING_STARTED] = true + return emptyList() + } + + val missed = db.getUnnotifiedIncomingMessages() + if (missed.isNotEmpty()) { + Log.d(TAG, "Replaying ${missed.size} message(s) stored while no handler was listening") + } + + return missed.groupBy(keySelector = { it.first }, valueTransform = { it.second }) + .map { (threadId, ids) -> MessageChanges(MessageChanges.ChangeType.Added, ids, threadId) } + } + + private fun SupportSQLiteDatabase.markAllUnreadNotified() { + for (isMms in listOf(false, true)) { + execSQL(""" + UPDATE ${tableName(isMms)} SET ${MmsSmsColumns.NOTIFIED} = 1 + WHERE ${MmsSmsColumns.READ} = 0 AND ${MmsSmsColumns.NOTIFIED} = 0 + """) + } + } + + /** + * Incoming messages that are unread and unmarked, with their threads. A message read on another + * device needs no notification. The read and notified tests match the existing + * (read, notified, thread_id) index on both tables. + */ + private fun SupportSQLiteDatabase.getUnnotifiedIncomingMessages(): List> { + val select = { isMms: Boolean -> + """ + SELECT ${MmsSmsColumns.THREAD_ID}, ${MmsSmsColumns.ID}, ${if (isMms) 1 else 0} + FROM ${tableName(isMms)} + WHERE ${MmsSmsColumns.READ} = 0 AND ${MmsSmsColumns.NOTIFIED} = 0 + AND NOT ${MmsSmsColumns.IS_OUTGOING} AND NOT ${MmsSmsColumns.IS_DELETED} + """ + } + + return query("${select(false)} UNION ALL ${select(true)}").use { cursor -> + val result = ArrayList>(cursor.count) + while (cursor.moveToNext()) { + result += cursor.getLong(0) to MessageId(cursor.getLong(1), mms = cursor.getInt(2) == 1) + } + result + } + } + + private fun tableName(isMms: Boolean): String = + if (isMms) MmsDatabase.TABLE_NAME else SmsDatabase.TABLE_NAME + + companion object { + private const val TAG = "MissedMessageEvents" + + private val TRACKING_STARTED = PreferenceKey.boolean("notification.missed_message_tracking_started") + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/NoNameOrContentNotificationHandler.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/NoNameOrContentNotificationHandler.kt index 8d803797a8..aedf66b088 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/NoNameOrContentNotificationHandler.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/NoNameOrContentNotificationHandler.kt @@ -14,7 +14,6 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.FlowCollector -import kotlinx.coroutines.flow.merge import network.loki.messenger.R import org.session.libsession.utilities.Address import org.session.libsignal.utilities.Log @@ -67,21 +66,30 @@ class NoNameOrContentNotificationHandler @Inject constructor( private val prefs: PreferenceStorage, private val appVisibilityManager: AppVisibilityManager, private val loginStateRepository: LoginStateRepository, + private val missedMessageEvents: MissedMessageEvents, ): BaseNotificationHandler( currentActivityObserver = currentActivityObserver, threadDb = threadDb, recipientRepository = recipientRepository, ) { suspend fun process() { - merge( - threadDb.changeNotification, - mmsDatabase.changeNotification, - smsDatabase.changeNotification, - reactionDatabase.changeNotification, + missedMessageEvents.withMissedMessages( + live = listOf( + threadDb.changeNotification, + mmsDatabase.changeNotification, + smsDatabase.changeNotification, + reactionDatabase.changeNotification, + ), + toEvent = { it }, ).collect(object : FlowCollector { private val lastNotifiedByThreadId = MutableLongLongMap() override suspend fun emit(value: Any) { + handle(value) + if (value is MessageChanges) missedMessageEvents.markSeen(value) + } + + private suspend fun handle(value: Any) { when (value) { is ThreadChanges -> { val newLastSeen = diff --git a/app/src/test/java/org/thoughtcrime/securesms/notifications/MissedMessageEventsTest.kt b/app/src/test/java/org/thoughtcrime/securesms/notifications/MissedMessageEventsTest.kt new file mode 100644 index 0000000000..a9c152378a --- /dev/null +++ b/app/src/test/java/org/thoughtcrime/securesms/notifications/MissedMessageEventsTest.kt @@ -0,0 +1,194 @@ +package org.thoughtcrime.securesms.notifications + +import android.content.ContentValues +import android.content.Context +import android.database.sqlite.SQLiteDatabase.CONFLICT_NONE +import androidx.sqlite.db.SupportSQLiteDatabase +import androidx.sqlite.db.SupportSQLiteOpenHelper +import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory +import androidx.test.core.app.ApplicationProvider +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.emptyFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.thoughtcrime.securesms.database.MmsDatabase +import org.thoughtcrime.securesms.database.MmsSmsColumns +import org.thoughtcrime.securesms.database.SmsDatabase +import org.thoughtcrime.securesms.database.model.MessageChanges +import org.thoughtcrime.securesms.database.model.MessageChanges.ChangeType +import org.thoughtcrime.securesms.database.model.MessageId +import org.thoughtcrime.securesms.preferences.PreferenceKey +import org.thoughtcrime.securesms.preferences.PreferenceStorage +import javax.inject.Provider + +@RunWith(RobolectricTestRunner::class) +@Config(minSdk = 36) // The sms and mms tables use generated columns, which need a recent sqlite +class MissedMessageEventsTest { + private val openHelper = createInMemoryMessageTables() + private val db get() = openHelper.writableDatabase + private val prefs = InMemoryPreferences() + private val events = MissedMessageEvents(prefs, Provider { openHelper }) + + @Test + fun `a message stored before subscribing is replayed as an Added event for its thread`() = runTest { + startAfterAnEarlierRun() + val sms = insertSms(threadId = 7) + val mms = insertMms(threadId = 7) + val otherThread = insertSms(threadId = 8) + + assertEquals( + setOf( + MessageChanges(ChangeType.Added, listOf(sms, mms), 7), + MessageChanges(ChangeType.Added, listOf(otherThread), 8), + ), + replayed().toSet() + ) + } + + @Test + fun `messages a handler has seen are not replayed`() = runTest { + startAfterAnEarlierRun() + val seen = insertSms(threadId = 7) + val seenMms = insertMms(threadId = 7) + val missed = insertSms(threadId = 7) + events.markSeen(MessageChanges(ChangeType.Added, listOf(seen, seenMms), 7)) + // Only Added events count. + events.markSeen(MessageChanges(ChangeType.Updated, missed, 7)) + + assertEquals(listOf(MessageChanges(ChangeType.Added, listOf(missed), 7)), replayed()) + } + + @Test + fun `a new message that reuses a deleted message's sms id is replayed`() = runTest { + startAfterAnEarlierRun() + val first = insertSms(threadId = 7) + events.markSeen(MessageChanges(ChangeType.Added, first, 7)) + db.delete(SmsDatabase.TABLE_NAME, "${MmsSmsColumns.ID} = ?", arrayOf(first.id)) + + val reused = insertSms(threadId = 7) + + assertEquals(first, reused) + assertEquals(listOf(MessageChanges(ChangeType.Added, listOf(reused), 7)), replayed()) + } + + @Test + fun `outgoing, deleted and already-read messages are not replayed`() = runTest { + startAfterAnEarlierRun() + insertSms(threadId = 7, type = SENT_TYPE, read = 1) + insertMms(threadId = 7, type = SENT_TYPE, read = 1) + insertSms(threadId = 7, type = DELETED_INCOMING_TYPE) + insertSms(threadId = 7, read = 1) + + assertEquals(emptyList(), replayed()) + } + + @Test + fun `the first run marks the stored messages without replaying them`() = runTest { + insertSms(threadId = 7) + insertMms(threadId = 7) + + assertEquals(emptyList(), replayed()) + + val next = insertSms(threadId = 7) + assertEquals(listOf(MessageChanges(ChangeType.Added, listOf(next), 7)), replayed()) + } + + @Test + fun `a message stored while the database is read still arrives live`() = runTest { + startAfterAnEarlierRun() + // The change flows keep no replay, so this only arrives if the subscription came first. + val live = MutableSharedFlow(extraBufferCapacity = 24) + val storedDuringRead = MessageChanges(ChangeType.Added, MessageId(11, false), 7) + val events = MissedMessageEvents(prefs, Provider { + check(live.subscriptionCount.value == 1) + live.tryEmit(storedDuringRead) + openHelper + }) + + assertEquals(storedDuringRead, events.withMissedMessages(listOf(live), toEvent = { it }).first()) + } + + /** The Added events one subscription replays. With no live flows, the flow ends after them. */ + private suspend fun replayed(): List = + events.withMissedMessages(emptyList>(), toEvent = { it }).toList() + + private fun startAfterAnEarlierRun() { + prefs[PreferenceKey.boolean("notification.missed_message_tracking_started")] = true + } + + private fun insertSms(threadId: Long, type: Long = INBOX_TYPE, read: Int = 0): MessageId { + val id = db.insert(SmsDatabase.TABLE_NAME, CONFLICT_NONE, ContentValues().apply { + put(MmsSmsColumns.THREAD_ID, threadId) + put(SmsDatabase.TYPE, type) + put(MmsSmsColumns.READ, read) + }) + return MessageId(id, mms = false) + } + + private fun insertMms(threadId: Long, type: Long = INBOX_TYPE, read: Int = 0): MessageId { + val id = db.insert(MmsDatabase.TABLE_NAME, CONFLICT_NONE, ContentValues().apply { + put(MmsSmsColumns.THREAD_ID, threadId) + put(MmsDatabase.MESSAGE_BOX, type) + put(MmsSmsColumns.READ, read) + }) + return MessageId(id, mms = true) + } + + private class InMemoryPreferences : PreferenceStorage { + private val values = mutableMapOf() + + override fun set(key: PreferenceKey, value: T) { + values[key.name] = value + } + + override fun remove(key: PreferenceKey<*>) { + values.remove(key.name) + } + + @Suppress("UNCHECKED_CAST") + override fun get(key: PreferenceKey): T { + if (key.name in values) return values[key.name] as T + return (key.strategy as PreferenceKey.Strategy.PrimitiveBoolean).defaultValue as T + } + + override fun changes(): Flow> = emptyFlow() + + override fun watch(scope: CoroutineScope, key: PreferenceKey): StateFlow = + throw UnsupportedOperationException() + } + + companion object { + // MmsSmsColumns.Types' base types, which are package-private. + private const val DELETED_INCOMING_TYPE = 19L + private const val INBOX_TYPE = 20L + private const val SENT_TYPE = 23L + + fun createInMemoryMessageTables(): SupportSQLiteOpenHelper { + val context = ApplicationProvider.getApplicationContext() + val config = SupportSQLiteOpenHelper.Configuration.builder(context) + .name(null) + .callback(object : SupportSQLiteOpenHelper.Callback(1) { + override fun onCreate(db: SupportSQLiteDatabase) { + db.execSQL(SmsDatabase.CREATE_TABLE) + SmsDatabase.addOutgoingColumn(db) + db.execSQL(MmsDatabase.CREATE_TABLE) + MmsDatabase.addOutgoingColumn(db) + } + + override fun onUpgrade(db: SupportSQLiteDatabase, oldVersion: Int, newVersion: Int) {} + }) + .build() + + return FrameworkSQLiteOpenHelperFactory().create(config) + } + } +}