Kotlin DSL for creating Android Notification using NotificationCompat.
AndroidX.Core variant in Java:
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
Android Notification DSL variant:
notification(context, CHANNEL_ID, smallIcon = R.drawable.notification_icon) {
contentTitle(textTitle)
contentText(textContent)
priority(NotificationCompat.PRIORITY_DEFAULT)
}
Create grouped notifications
notificationsGroup(context, groupKey = GROUP_KEY, channelId = CHANNEL) {
summary(SUMMARY_NOTIFICATION_ID, smallIcon = R.drawable.ic_android_white_24dp) {
contentTitle(R.string.notification_summary_title)
contentText(R.string.notification_summary_text)
}
notifications {
notification(NOTIFICATION_1_ID, smallIcon = R.drawable.ic_android_white_24dp) {
contentTitle(R.string.notification_1_title)
}
notification(NOTIFICATION_2_ID, smallIcon = R.drawable.ic_android_white_24dp) {
contentTitle(R.string.notification_2_title)
}
}
}
Creating Android Notification Channels and Groups
createChannelsAndGroups(context) {
channel(CHANNEL_1_ID, CHANNEL_1_NAME)
group(CHANNEL_GROUP_1_ID, CHANNEL_GROUP_1_NAME) {
// Empty group
}
group(CHANNEL_GROUP_2_ID, CHANNEL_GROUP_2_NAME) {
channel(CHANNEL_2_ID, CHANNEL_2_NAME)
}
}