From 55804e55760e1dcf2ace9ea5ab602fe7718a94a9 Mon Sep 17 00:00:00 2001 From: Senne Janssens Date: Wed, 25 Nov 2026 14:57:04 +0100 Subject: [PATCH] feat: Add delete action to message notifications --- app/src/main/AndroidManifest.xml | 3 + .../com/github/gotify/NotificationSupport.kt | 1 + .../service/NotificationActionReceiver.kt | 70 +++++++++++++++++++ .../github/gotify/service/WebSocketService.kt | 18 ++++- 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/github/gotify/service/NotificationActionReceiver.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5f08400e..9a30af13 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -95,6 +95,9 @@ + diff --git a/app/src/main/kotlin/com/github/gotify/NotificationSupport.kt b/app/src/main/kotlin/com/github/gotify/NotificationSupport.kt index 77cc0b16..99848026 100644 --- a/app/src/main/kotlin/com/github/gotify/NotificationSupport.kt +++ b/app/src/main/kotlin/com/github/gotify/NotificationSupport.kt @@ -229,5 +229,6 @@ internal object NotificationSupport { object ID { const val FOREGROUND = -1 const val GROUPED = -2 + const val GROUP_SUMMARY = -5 } } diff --git a/app/src/main/kotlin/com/github/gotify/service/NotificationActionReceiver.kt b/app/src/main/kotlin/com/github/gotify/service/NotificationActionReceiver.kt new file mode 100644 index 00000000..0486f7de --- /dev/null +++ b/app/src/main/kotlin/com/github/gotify/service/NotificationActionReceiver.kt @@ -0,0 +1,70 @@ +package com.github.gotify.service + +import android.app.Notification +import android.app.NotificationManager +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import com.github.gotify.NotificationSupport +import com.github.gotify.Settings +import com.github.gotify.Utils +import com.github.gotify.api.Callback +import com.github.gotify.api.ClientFactory +import com.github.gotify.client.api.MessageApi +import org.tinylog.kotlin.Logger + +internal class NotificationActionReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val messageId = intent.getLongExtra(EXTRA_MESSAGE_ID, INVALID_MESSAGE_ID) + if (messageId <= 0) { + Logger.warn("Ignoring delete notification action with invalid message id") + return + } + + val settings = Settings(context) + if (!settings.tokenExists()) { + Logger.warn("Cannot delete notification because no client token is configured") + return + } + + val pendingResult = goAsync() + ClientFactory.clientToken(settings) + .createService(MessageApi::class.java) + .deleteMessage(messageId) + .enqueue( + Callback.call( + onSuccess = { + try { + val notificationManager = + context.getSystemService( + Context.NOTIFICATION_SERVICE + ) as NotificationManager + notificationManager.cancel(Utils.longToInt(messageId)) + val hasMessageNotifications = + notificationManager.activeNotifications.any { statusBarNotification -> + val notification = statusBarNotification.notification + notification.group == NotificationSupport.Group.MESSAGES && + (notification.flags and + Notification.FLAG_GROUP_SUMMARY) == 0 + } + if (!hasMessageNotifications) { + notificationManager.cancel( + NotificationSupport.ID.GROUP_SUMMARY + ) + } + } finally { + pendingResult.finish() + } + }, + onError = { + pendingResult.finish() + } + ) + ) + } + + companion object { + const val EXTRA_MESSAGE_ID = "com.github.gotify.MESSAGE_ID" + private const val INVALID_MESSAGE_ID = -1L + } +} diff --git a/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt b/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt index 0e5d6dd0..bbbd4d05 100644 --- a/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt +++ b/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt @@ -420,6 +420,22 @@ internal class WebSocketService : Service() { b.setContentText(newMessage ?: message) b.setStyle(NotificationCompat.BigTextStyle().bigText(formattedMessage)) + if (id > 0) { + val deleteIntent = Intent(this, NotificationActionReceiver::class.java) + .putExtra(NotificationActionReceiver.EXTRA_MESSAGE_ID, id) + val deletePendingIntent = PendingIntent.getBroadcast( + this, + Utils.longToInt(id), + deleteIntent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + ) + b.addAction( + R.drawable.ic_delete, + getString(R.string.delete_message), + deletePendingIntent + ) + } + val notificationImageUrl = Extras.getNestedValue( String::class.java, extras, @@ -470,6 +486,6 @@ internal class WebSocketService : Service() { .setContentIntent(contentIntent) val notificationManager = this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager - notificationManager.notify(-5, builder.build()) + notificationManager.notify(NotificationSupport.ID.GROUP_SUMMARY, builder.build()) } }