separate notification group creation and deletion from channel creation and deletion

This commit is contained in:
Markus Doits 2022-12-06 19:15:47 +01:00
parent a94d750b73
commit 522f05ed60
No known key found for this signature in database
GPG key ID: 4D406BBC615201D1

View file

@ -57,46 +57,48 @@ class NotificationService(val context: Context) {
} }
fun createDefaultNotificationChannels() { fun createDefaultNotificationChannels() {
maybeCreateNotificationGroup(DEFAULT_NOTIFICATION_SCOPE, context.getString(R.string.channel_notifications_group_default_name))
(1..5).forEach { priority -> (1..5).forEach { priority ->
maybeCreateNotificationChannel( maybeCreateNotificationChannel(
DEFAULT_NOTIFICATION_SCOPE, DEFAULT_NOTIFICATION_SCOPE,
priority, priority,
DEFAULT_NOTIFICATION_SCOPE, // use default scope as group id DEFAULT_NOTIFICATION_SCOPE // use default scope as group id
context.getString(R.string.channel_notifications_group_default_name)
) )
} }
} }
fun createSubscriptionNotificationChannels(subscription: Subscription) { fun createSubscriptionNotificationChannels(subscription: Subscription) {
val notificationScope = dedicatedNotificationScope(subscription) val notificationScope = dedicatedNotificationScope(subscription)
val groupId = groupId(subscription) val groupId = dedicatedGroupId(subscription)
val displayName = displayName(subscription) val displayName = displayName(subscription)
(1..5).forEach { priority -> maybeCreateNotificationChannel(notificationScope, priority, groupId, displayName) } maybeCreateNotificationGroup(groupId, displayName)
(1..5).forEach { priority -> maybeCreateNotificationChannel(notificationScope, priority, groupId) }
} }
fun deleteSubscriptionNotificationChannels(subscription: Subscription) { fun deleteSubscriptionNotificationChannels(subscription: Subscription) {
val notificationScope = dedicatedNotificationScope(subscription) val notificationScope = dedicatedNotificationScope(subscription)
val groupId = groupId(subscription) val groupId = dedicatedGroupId(subscription)
(1..5).forEach { priority -> maybeDeleteNotificationChannel(notificationScope, priority, groupId) } (1..5).forEach { priority -> maybeDeleteNotificationChannel(notificationScope, priority) }
maybeDeleteNotificationGroup(groupId)
} }
fun dedicatedNotificationScope(subscription: Subscription): String { fun dedicatedNotificationScope(subscription: Subscription): String {
return "" + subscription.id return "" + subscription.id
} }
fun groupId(subscription: Subscription): String? { fun dedicatedGroupId(subscription: Subscription): String {
if (subscription.dedicatedChannels) { return "" + subscription.id
return "" + subscription.id
} else {
return null
}
} }
private fun displayInternal(subscription: Subscription, notification: Notification, update: Boolean = false) { private fun displayInternal(subscription: Subscription, notification: Notification, update: Boolean = false) {
val title = formatTitle(subscription, notification) val title = formatTitle(subscription, notification)
val scope = if (subscription.dedicatedChannels) dedicatedNotificationScope(subscription) else DEFAULT_NOTIFICATION_SCOPE val scope = if (subscription.dedicatedChannels) dedicatedNotificationScope(subscription) else DEFAULT_NOTIFICATION_SCOPE
val groupId = if (subscription.dedicatedChannels) dedicatedGroupId(subscription) else null
val builder = NotificationCompat.Builder(context, toChannelId(scope, notification.priority)) val builder = NotificationCompat.Builder(context, toChannelId(scope, notification.priority))
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setColor(ContextCompat.getColor(context, Colors.notificationIcon(context))) .setColor(ContextCompat.getColor(context, Colors.notificationIcon(context)))
@ -113,7 +115,12 @@ class NotificationService(val context: Context) {
maybeAddCancelAction(builder, notification) maybeAddCancelAction(builder, notification)
maybeAddUserActions(builder, notification) maybeAddUserActions(builder, notification)
maybeCreateNotificationChannel(scope, notification.priority, groupId(subscription), displayName(subscription)) if (groupId != null) {
maybeCreateNotificationGroup(groupId, displayName(subscription))
}
maybeCreateNotificationChannel(scope, notification.priority, groupId)
notificationManager.notify(notification.notificationId, builder.build()) notificationManager.notify(notification.notificationId, builder.build())
} }
@ -346,7 +353,7 @@ class NotificationService(val context: Context) {
} }
} }
private fun maybeCreateNotificationChannel(scope: String, priority: Int, groupId: String?, groupName: String?=null) { private fun maybeCreateNotificationChannel(scope: String, priority: Int, groupId: String?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Note: To change a notification channel, you must delete the old one and create a new one! // Note: To change a notification channel, you must delete the old one and create a new one!
@ -380,8 +387,7 @@ class NotificationService(val context: Context) {
else -> NotificationChannel(toChannelId(scope, priority), context.getString(R.string.channel_notifications_default_name), NotificationManager.IMPORTANCE_DEFAULT) else -> NotificationChannel(toChannelId(scope, priority), context.getString(R.string.channel_notifications_default_name), NotificationManager.IMPORTANCE_DEFAULT)
} }
if (groupId != null && groupName != null) { if (groupId != null) {
notificationManager.createNotificationChannelGroup(NotificationChannelGroup(groupId, groupName))
channel.setGroup(groupId) channel.setGroup(groupId)
} }
@ -389,13 +395,21 @@ class NotificationService(val context: Context) {
} }
} }
private fun maybeDeleteNotificationChannel(scope: String, priority: Int, groupId: String?) { private fun maybeDeleteNotificationChannel(scope: String, priority: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.deleteNotificationChannel(toChannelId(scope, priority)) notificationManager.deleteNotificationChannel(toChannelId(scope, priority))
}
}
if (groupId != null) { private fun maybeCreateNotificationGroup(id: String, name: String) {
notificationManager.deleteNotificationChannelGroup(groupId) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
} notificationManager.createNotificationChannelGroup(NotificationChannelGroup(id, name))
}
}
private fun maybeDeleteNotificationGroup(id: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.deleteNotificationChannelGroup(id)
} }
} }