Skip to content

Commit 8bb3fb1

Browse files
committed
Allow custom notification channel
1 parent 6c212d2 commit 8bb3fb1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

acra-notification/src/main/java/org/acra/config/NotificationConfiguration.kt

+15-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.acra.config
1919
import androidx.annotation.ColorInt
2020
import androidx.annotation.DrawableRes
2121
import com.faendir.kotlin.autodsl.AutoDsl
22+
import com.faendir.kotlin.autodsl.AutoDslRequired
2223
import org.acra.annotation.AcraDsl
2324
import org.acra.ktx.plus
2425

@@ -103,14 +104,27 @@ class NotificationConfiguration(
103104
@DrawableRes
104105
val resDiscardButtonIcon: Int = android.R.drawable.ic_menu_delete,
105106

107+
/**
108+
* Existing notification channel id.
109+
* You have to ensure a notification channel with this id exists (See [android.app.NotificationManager.createNotificationChannel]).
110+
* If null, ACRA will create a notification channel for you with the provided [channelName], [channelDescription] and [channelImportance].
111+
* To learn about notification channels, visit the [notification guide](https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels)
112+
*
113+
* @see android.app.NotificationChannel
114+
* @since 5.12.0
115+
*/
116+
@AutoDslRequired("channel")
117+
val channelId: String? = null,
118+
106119
/**
107120
* notification channel name.
108121
* To learn about notification channels, visit the [notification guide](https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels)
109122
*
110123
* @see android.app.NotificationChannel
111124
* @since 5.0.0
112125
*/
113-
val channelName: String,
126+
@AutoDslRequired("channel")
127+
val channelName: String? = null,
114128

115129
/**
116130
* notification channel description

acra-notification/src/main/java/org/acra/interaction/NotificationInteraction.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class NotificationInteraction : HasConfigPlugin(NotificationConfiguration::class
5454
//can't post notifications
5555
val notificationConfig = config.getPluginConfiguration<NotificationConfiguration>()
5656
//We have to create a channel on Oreo+, because notifications without one aren't allowed
57-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
57+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationConfig.channelId == null) {
5858
@SuppressLint("WrongConstant")
5959
val channel = NotificationChannel(CHANNEL, notificationConfig.channelName, notificationConfig.channelImportance)
6060
channel.setSound(null, null)
@@ -64,7 +64,7 @@ class NotificationInteraction : HasConfigPlugin(NotificationConfiguration::class
6464
notificationManager.createNotificationChannel(channel)
6565
}
6666
//configure base notification
67-
val notification = NotificationCompat.Builder(context, CHANNEL)
67+
val notification = NotificationCompat.Builder(context, notificationConfig.channelId ?: CHANNEL)
6868
.setWhen(System.currentTimeMillis())
6969
.setContentTitle(notificationConfig.title)
7070
.setContentText(notificationConfig.text)

0 commit comments

Comments
 (0)