From 069cbe2c9bb3b19860a61989990144b02c1f4213 Mon Sep 17 00:00:00 2001 From: daladim Date: Sun, 3 Aug 2025 16:53:54 -0400 Subject: [PATCH] Notifications use channel names, based on Caldav calendar names As far as I can tell, this does not need to actually come from a synced Caldav server. Local lists also behave internally as caldav calendars. --- .../notifications/NotificationManager.kt | 22 ++++++++++++++++++- .../kotlin/org/tasks/data/dao/CaldavDao.kt | 7 ++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/tasks/notifications/NotificationManager.kt b/app/src/main/java/org/tasks/notifications/NotificationManager.kt index 9f93139e4..96f408520 100644 --- a/app/src/main/java/org/tasks/notifications/NotificationManager.kt +++ b/app/src/main/java/org/tasks/notifications/NotificationManager.kt @@ -2,6 +2,7 @@ package org.tasks.notifications import android.Manifest import android.annotation.SuppressLint +import android.app.NotificationChannel import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -15,6 +16,7 @@ import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch import org.tasks.LocalBroadcastManager import org.tasks.R +import org.tasks.data.dao.CaldavDao import org.tasks.data.dao.LocationDao import org.tasks.data.dao.NotificationDao import org.tasks.data.dao.TaskDao @@ -44,6 +46,7 @@ class NotificationManager @Inject constructor( private val preferences: Preferences, private val notificationDao: NotificationDao, private val taskDao: TaskDao, + private val caldavDao: CaldavDao, private val locationDao: LocationDao, private val localBroadcastManager: LocalBroadcastManager, private val notificationManager: ThrottledNotificationManager, @@ -348,8 +351,24 @@ class NotificationManager @Inject constructor( val markdown = markdownProvider.markdown(force = true) val taskTitle = markdown.toMarkdown(task.title) val taskDescription = markdown.toMarkdown(task.notes) + val calendarName = caldavDao.getCalendarNameForTask(id) ?: "No calendar name" - val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_DEFAULT) + // Create an Android notification channel for this list + // This gives the user a way to enable notifications only for some lists + val channelId = "$NOTIFICATION_CHANNEL_PREFIX$calendarName" + + // TODO: de-duplicate with createNotificationChannel from NotificationSchedulerIntentService + val importance = android.app.NotificationManager.IMPORTANCE_HIGH + val notificationChannel = NotificationChannel(channelId, calendarName, importance) + notificationChannel.enableLights(true) + notificationChannel.enableVibration(true) + notificationChannel.setBypassDnd(true) + notificationChannel.setShowBadge(true) + + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager + notificationManager.createNotificationChannel(notificationChannel) + + val builder = NotificationCompat.Builder(context, calendarName) .setCategory(NotificationCompat.CATEGORY_REMINDER) .setContentTitle(taskTitle) .setColor(colorProvider.getPriorityColor(task.priority)) @@ -481,6 +500,7 @@ class NotificationManager @Inject constructor( companion object { const val NOTIFICATION_CHANNEL_DEFAULT = "notifications" + const val NOTIFICATION_CHANNEL_PREFIX = "notifications_" const val NOTIFICATION_CHANNEL_TASKER = "notifications_tasker" const val NOTIFICATION_CHANNEL_TIMERS = "notifications_timers" const val NOTIFICATION_CHANNEL_MISCELLANEOUS = "notifications_miscellaneous" diff --git a/data/src/commonMain/kotlin/org/tasks/data/dao/CaldavDao.kt b/data/src/commonMain/kotlin/org/tasks/data/dao/CaldavDao.kt index 04537449c..3e7749078 100644 --- a/data/src/commonMain/kotlin/org/tasks/data/dao/CaldavDao.kt +++ b/data/src/commonMain/kotlin/org/tasks/data/dao/CaldavDao.kt @@ -192,6 +192,13 @@ WHERE cd_calendar = :calendar @Query("SELECT * FROM caldav_tasks WHERE cd_task = :taskId") abstract suspend fun getTasks(taskId: Long): List + @Query(""" + SELECT caldav_lists.cdl_name FROM caldav_tasks + JOIN caldav_lists ON caldav_tasks.cd_calendar = caldav_lists.cdl_uuid + WHERE caldav_tasks.cd_id = :taskId + """) + abstract suspend fun getCalendarNameForTask(taskId: Long): String? + @Query(""" SELECT EXISTS(SELECT 1 FROM caldav_tasks