simplify notification channels creation

This commit is contained in:
Adam Brown 2022-11-03 17:10:29 +00:00
parent ab330c94d9
commit c6a6dd9bd3
1 changed files with 20 additions and 45 deletions

View File

@ -1,5 +1,6 @@
package app.dapk.st.notifications package app.dapk.st.notifications
import android.annotation.SuppressLint
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationChannelGroup import android.app.NotificationChannelGroup
import android.app.NotificationManager import android.app.NotificationManager
@ -20,55 +21,29 @@ class NotificationChannels(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannelGroup(NotificationChannelGroup(CHATS_NOTIFICATION_GROUP_ID, "Chats")) notificationManager.createNotificationChannelGroup(NotificationChannelGroup(CHATS_NOTIFICATION_GROUP_ID, "Chats"))
if (notificationManager.getNotificationChannel(DIRECT_CHANNEL_ID) == null) { notificationManager.createIfMissing(DIRECT_CHANNEL_ID) {
notificationManager.createNotificationChannel( createChannel(it, "Direct notifications", NotificationManager.IMPORTANCE_HIGH, CHATS_NOTIFICATION_GROUP_ID)
NotificationChannel( }
DIRECT_CHANNEL_ID, notificationManager.createIfMissing(GROUP_CHANNEL_ID) {
"Direct notifications", createChannel(it, "Group notifications", NotificationManager.IMPORTANCE_HIGH, CHATS_NOTIFICATION_GROUP_ID)
NotificationManager.IMPORTANCE_HIGH, }
).also { notificationManager.createIfMissing(INVITE_CHANNEL_ID) {
it.enableVibration(true) createChannel(it, "Invite notifications", NotificationManager.IMPORTANCE_DEFAULT, CHATS_NOTIFICATION_GROUP_ID)
it.enableLights(true) }
it.group = CHATS_NOTIFICATION_GROUP_ID notificationManager.createIfMissing(SUMMARY_CHANNEL_ID) {
createChannel(it, "Other notifications", NotificationManager.IMPORTANCE_DEFAULT, CHATS_NOTIFICATION_GROUP_ID)
}
} }
)
} }
if (notificationManager.getNotificationChannel(GROUP_CHANNEL_ID) == null) { @SuppressLint("NewApi")
notificationManager.createNotificationChannel( private fun createChannel(id: String, name: String, importance: Int, groupId: String) = NotificationChannel(id, name, importance)
NotificationChannel( .also { it.group = groupId }
GROUP_CHANNEL_ID,
"Group notifications",
NotificationManager.IMPORTANCE_HIGH,
).also {
it.group = CHATS_NOTIFICATION_GROUP_ID
}
)
}
if (notificationManager.getNotificationChannel(INVITE_CHANNEL_ID) == null) { @SuppressLint("NewApi")
notificationManager.createNotificationChannel( private fun NotificationManager.createIfMissing(id: String, creator: (String) -> NotificationChannel) {
NotificationChannel( if (this.getNotificationChannel(SUMMARY_CHANNEL_ID) == null) {
INVITE_CHANNEL_ID, this.createNotificationChannel(creator.invoke(id))
"Invite notifications",
NotificationManager.IMPORTANCE_DEFAULT,
).also {
it.group = CHATS_NOTIFICATION_GROUP_ID
}
)
}
if (notificationManager.getNotificationChannel(SUMMARY_CHANNEL_ID) == null) {
notificationManager.createNotificationChannel(
NotificationChannel(
SUMMARY_CHANNEL_ID,
"Other notifications",
NotificationManager.IMPORTANCE_DEFAULT,
).also {
it.group = CHATS_NOTIFICATION_GROUP_ID
}
)
}
} }
} }