From 3daa41f89a7c4f9166ff41b8a3c6143c3d5a1dd3 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Fri, 6 Feb 2026 11:54:20 -0800 Subject: [PATCH] Fix clear=true --- .../java/io/heckel/ntfy/app/Application.kt | 5 +++++ .../io/heckel/ntfy/msg/NotificationService.kt | 21 +++++++++++++++++-- .../io/heckel/ntfy/msg/UserActionWorker.kt | 2 ++ .../metadata/android/en-US/changelog/NEXT.txt | 3 ++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/app/Application.kt b/app/src/main/java/io/heckel/ntfy/app/Application.kt index 605a2e90..d594d670 100644 --- a/app/src/main/java/io/heckel/ntfy/app/Application.kt +++ b/app/src/main/java/io/heckel/ntfy/app/Application.kt @@ -4,8 +4,13 @@ import android.app.Application import com.google.android.material.color.DynamicColors import io.heckel.ntfy.db.Repository import io.heckel.ntfy.util.Log +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob class Application : Application() { + val ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) + val repository by lazy { val repository = Repository.getInstance(applicationContext) if (repository.getRecordLogs()) { diff --git a/app/src/main/java/io/heckel/ntfy/msg/NotificationService.kt b/app/src/main/java/io/heckel/ntfy/msg/NotificationService.kt index 86b3cdd3..1d621ecc 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/NotificationService.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/NotificationService.kt @@ -21,6 +21,7 @@ import io.heckel.ntfy.ui.MainActivity import io.heckel.ntfy.util.* import java.util.* import androidx.core.net.toUri +import kotlinx.coroutines.launch class NotificationService(val context: Context) { private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager @@ -135,8 +136,8 @@ class NotificationService(val context: Context) { private fun setStyleAndText(builder: NotificationCompat.Builder, subscription: Subscription, notification: Notification) { val contentUri = notification.attachment?.contentUri val isSupportedImage = supportedImage(notification.attachment?.type) - val subscriptionIcon = if (subscription.icon != null) subscription.icon.readBitmapFromUriOrNull(context) else null - val notificationIcon = if (notification.icon != null) notification.icon.contentUri?.readBitmapFromUriOrNull(context) else null + val subscriptionIcon = subscription.icon?.readBitmapFromUriOrNull(context) + val notificationIcon = notification.icon?.contentUri?.readBitmapFromUriOrNull(context) val largeIcon = notificationIcon ?: subscriptionIcon if (contentUri != null && isSupportedImage) { try { @@ -299,6 +300,8 @@ class NotificationService(val context: Context) { addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) putExtra(VIEW_ACTION_EXTRA_URL, url) putExtra(VIEW_ACTION_EXTRA_NOTIFICATION_ID, notification.notificationId) + putExtra(VIEW_ACTION_EXTRA_SUBSCRIPTION_ID, notification.subscriptionId) + putExtra(VIEW_ACTION_EXTRA_SEQUENCE_ID, notification.sequenceId) } val pendingIntent = PendingIntent.getActivity(context, Random().nextInt(), intent, PendingIntent.FLAG_IMMUTABLE) builder.addAction(NotificationCompat.Action.Builder(0, action.label, pendingIntent).build()) @@ -469,6 +472,8 @@ class NotificationService(val context: Context) { Log.d(TAG, "Created $this") val url = intent.getStringExtra(VIEW_ACTION_EXTRA_URL) val notificationId = intent.getIntExtra(VIEW_ACTION_EXTRA_NOTIFICATION_ID, 0) + val subscriptionId = intent.getLongExtra(VIEW_ACTION_EXTRA_SUBSCRIPTION_ID, 0) + val sequenceId = intent.getStringExtra(VIEW_ACTION_EXTRA_SEQUENCE_ID) ?: "" if (url == null) { finish() return @@ -492,6 +497,16 @@ class NotificationService(val context: Context) { val notifier = NotificationService(this) notifier.cancel(notificationId) + // Mark notification as read; We can't use lifecycleScope here, because we + // call finish() right after, so we do this awkward ioScope thing. + if (subscriptionId != 0L && sequenceId.isNotEmpty()) { + val app = applicationContext as io.heckel.ntfy.app.Application + app.ioScope.launch { + val repository = Repository.getInstance(app) + repository.markAsReadBySequenceId(subscriptionId, sequenceId) + } + } + // Close this activity finish() } @@ -529,5 +544,7 @@ class NotificationService(val context: Context) { private const val VIEW_ACTION_EXTRA_URL = "url" private const val VIEW_ACTION_EXTRA_NOTIFICATION_ID = "notificationId" + private const val VIEW_ACTION_EXTRA_SUBSCRIPTION_ID = "subscriptionId" + private const val VIEW_ACTION_EXTRA_SEQUENCE_ID = "sequenceId" } } diff --git a/app/src/main/java/io/heckel/ntfy/msg/UserActionWorker.kt b/app/src/main/java/io/heckel/ntfy/msg/UserActionWorker.kt index b8b84b2f..a31c4015 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/UserActionWorker.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/UserActionWorker.kt @@ -60,6 +60,7 @@ class UserActionWorker(private val context: Context, params: WorkerParameters) : broadcaster.sendUserAction(action) if (action.clear == true) { notifier.cancel(notification) + repository.markAsReadBySequenceId(subscription.id, notification.sequenceId) } } @@ -98,6 +99,7 @@ class UserActionWorker(private val context: Context, params: WorkerParameters) : repository.updateNotification(notification) if (clear) { notifier.cancel(notification) + repository.markAsReadBySequenceId(subscription.id, notification.sequenceId) } else { notifier.update(subscription, notification) } diff --git a/fastlane/metadata/android/en-US/changelog/NEXT.txt b/fastlane/metadata/android/en-US/changelog/NEXT.txt index ad64abad..428ece57 100644 --- a/fastlane/metadata/android/en-US/changelog/NEXT.txt +++ b/fastlane/metadata/android/en-US/changelog/NEXT.txt @@ -5,6 +5,7 @@ Features: * Show last notification time for UnifiedPush subscriptions (#1230, #1454, thanks to @Tealk and @user4andre for reporting) Bug fixes + maintenance: +* Fix clear=true on action buttons not marking notification as read (#1029, thanks to @ElFishi for reporting) * Fix crash when URL scheme is missing in default server (#1582, ntfy-android#158, thanks to @hard-zero1 for reporting) * Fix notification timestamp to use original send time instead of receive time (#1112, thanks to @voruti for reporting) -* Fix notifications being missed after service restart (#1591, thanks to @Epifeny for reporting) \ No newline at end of file +* Fix notifications being missed after service restart (#1591, thanks to @Epifeny for reporting)