From 077e117e1010c323ef50ab32736a5fc20832b798 Mon Sep 17 00:00:00 2001 From: sim Date: Tue, 3 Dec 2024 09:33:37 +0000 Subject: [PATCH] Delete os notification channel when unused When deleting local channel from the UI --- .../distributor/nextpush/LocalNotification.kt | 19 +++++++++++++++++++ .../nextpush/distributor/Distributor.kt | 16 ++++++++++++++-- .../nextpush/utils/Notifications.kt | 5 +++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/LocalNotification.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/LocalNotification.kt index 513e2d2..a3b1461 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/LocalNotification.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/LocalNotification.kt @@ -1,6 +1,9 @@ package org.unifiedpush.distributor.nextpush +import android.app.NotificationManager import android.content.Context +import android.os.Build +import androidx.annotation.RequiresApi import java.util.UUID import org.unifiedpush.distributor.nextpush.Database.Companion.getDb import org.unifiedpush.distributor.nextpush.api.Api @@ -20,4 +23,20 @@ object LocalNotification { val title = getDb(context).getNotificationTitle(connectorToken) ?: context.getString(R.string.app_name) FromPushNotification(context, title, content).showBig() } + + /** + * Remove the notification channel registered to the OS + */ + @RequiresApi(Build.VERSION_CODES.O) + fun removeOsNotificationChannel(context: Context, connectorToken: String) { + val title = getDb(context).getNotificationTitle(connectorToken) ?: return + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + notificationManager.deleteNotificationChannel( + getNotificationChannelId(context, title) + ) + } + + fun getNotificationChannelId(context: Context, title: String): String { + return "${context.getString(R.string.app_name)}.Push.$title" + } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt index b3f98d8..e64e81a 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt @@ -2,6 +2,7 @@ package org.unifiedpush.distributor.nextpush.distributor import android.content.Context import android.content.Intent +import android.os.Build import android.util.Log import org.unifiedpush.distributor.nextpush.Database import org.unifiedpush.distributor.nextpush.Database.Companion.getDb @@ -61,9 +62,20 @@ object Distributor { context.sendBroadcast(broadcastIntent) } + /** + * Send UNREGISTERED action. + * + * If this is for a local application: we remove the notification channel instead + */ private fun sendUnregistered(context: Context, connectorToken: String) { - val application = getApp(context, connectorToken) - if (application.isNullOrBlank()) { + val application = getApp(context, connectorToken) ?: return + if (application == context.packageName) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + LocalNotification.removeOsNotificationChannel( + context, + connectorToken + ) + } return } val broadcastIntent = Intent() diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/Notifications.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/Notifications.kt index 9d12d3e..20653be 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/Notifications.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/Notifications.kt @@ -14,6 +14,7 @@ import androidx.core.app.ActivityCompat import androidx.core.app.NotificationManagerCompat import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger +import org.unifiedpush.distributor.nextpush.LocalNotification import org.unifiedpush.distributor.nextpush.R import org.unifiedpush.distributor.nextpush.activities.MainActivity import org.unifiedpush.distributor.nextpush.services.RegistrationCountCache @@ -280,8 +281,8 @@ class FromPushNotification(context: Context, title: String, content: String) : A false ), ChannelData( - "${context.getString(R.string.app_name)}.Push.$title", - "(Push) $title", + LocalNotification.getNotificationChannelId(context, title), + context.getString(R.string.list_registrations_local_title, title), NotificationManager.IMPORTANCE_HIGH, context.getString(R.string.list_registrations_local_description) )