Skip to content

Instantly share code, notes, and snippets.

@hotstu
Last active May 14, 2019 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotstu/732f3e64135ca3349a1de79d28f9a931 to your computer and use it in GitHub Desktop.
Save hotstu/732f3e64135ca3349a1de79d28f9a931 to your computer and use it in GitHub Desktop.
NotificationChannelBuilder, DSL style android notification channel builder powered by kotlin, simple way to support Android O notification channels
/**
* @author hglf <a href="https://github.com/hotstu">hglf</a>
* @since 5/13/19
* @desc
*/
class Config {
val chidren: ArrayList<NotificationChannelGroup> = ArrayList()
}
//NOTE: extension properies are resolved static, so here is a little mem leak, call clean() when every thing is done
val extendPenddingChannels = HashMap<NotificationChannelGroup, ArrayList<NotificationChannel>>()
val NotificationChannelGroup.penddingChannels: ArrayList<NotificationChannel>
get() = if (extendPenddingChannels[this] == null) {
val value = ArrayList<NotificationChannel>()
extendPenddingChannels[this] = value
value
} else {
extendPenddingChannels[this]!!
}
fun config(init: Config.() -> Unit): Config {
val config = Config()
config.apply(init)
return config
}
@RequiresApi(Build.VERSION_CODES.O)
fun Config.applyTo(notificationManager: NotificationManager) {
notificationManager.createNotificationChannelGroups(chidren)
notificationManager.createNotificationChannels(chidren.flatMap { it.penddingChannels })
}
fun Config.clean() {
extendPenddingChannels.clear()
}
@RequiresApi(Build.VERSION_CODES.O)
fun Config.group(
groupId: String = "default",
groupName: String = "default",
init: NotificationChannelGroup.() -> Unit): NotificationChannelGroup {
val group = NotificationChannelGroup(groupId, groupName)
chidren.add(group)
group.apply(init)
return group
}
@RequiresApi(Build.VERSION_CODES.O)
fun NotificationChannelGroup.channel(
id: String = "default",
name: String = "default",
importance: Int = NotificationManager.IMPORTANCE_DEFAULT,
init: NotificationChannel.() -> Unit): NotificationChannel {
val channel = NotificationChannel(id, name, importance)
channel.group = this.id
this.penddingChannels.add(channel)
channel.apply(init)
return channel
}
/**
* @author hglf <a href="https://github.com/hotstu">hglf</a>
* @since 5/14/19
* @desc
*/
class App : Application() {
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val config = config {
group(groupId = "chatGroup", groupName = "chat") {
channel(id = "Ch1") {
name = "channel1"
description = "when some chat request come"
}
channel(id = "Ch2") {
name = "channel2"
description = "when some important chat request come"
importance = NotificationManager.IMPORTANCE_HIGH
}
}
group(groupId = "noticeGroup", groupName = "notice") {
channel(id = "Ch3") {
name = "personal message"
description = "send personal message"
}
channel(id = "Ch4") {
name = "broadcast message"
description = "send broadcast"
}
}
}
config.applyTo(notificationManager)
config.clean()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment