Skip to content

Commit

Permalink
Allow custom notification channel
Browse files Browse the repository at this point in the history
  • Loading branch information
F43nd1r committed Nov 3, 2024
1 parent 6c212d2 commit 8bb3fb1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.acra.config
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import com.faendir.kotlin.autodsl.AutoDsl
import com.faendir.kotlin.autodsl.AutoDslRequired
import org.acra.annotation.AcraDsl
import org.acra.ktx.plus

Expand Down Expand Up @@ -103,14 +104,27 @@ class NotificationConfiguration(
@DrawableRes
val resDiscardButtonIcon: Int = android.R.drawable.ic_menu_delete,

/**
* Existing notification channel id.
* You have to ensure a notification channel with this id exists (See [android.app.NotificationManager.createNotificationChannel]).
* If null, ACRA will create a notification channel for you with the provided [channelName], [channelDescription] and [channelImportance].
* To learn about notification channels, visit the [notification guide](https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels)
*
* @see android.app.NotificationChannel
* @since 5.12.0
*/
@AutoDslRequired("channel")
val channelId: String? = null,

/**
* notification channel name.
* To learn about notification channels, visit the [notification guide](https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels)
*
* @see android.app.NotificationChannel
* @since 5.0.0
*/
val channelName: String,
@AutoDslRequired("channel")
val channelName: String? = null,

/**
* notification channel description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class NotificationInteraction : HasConfigPlugin(NotificationConfiguration::class
//can't post notifications
val notificationConfig = config.getPluginConfiguration<NotificationConfiguration>()
//We have to create a channel on Oreo+, because notifications without one aren't allowed
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationConfig.channelId == null) {
@SuppressLint("WrongConstant")
val channel = NotificationChannel(CHANNEL, notificationConfig.channelName, notificationConfig.channelImportance)
channel.setSound(null, null)
Expand All @@ -64,7 +64,7 @@ class NotificationInteraction : HasConfigPlugin(NotificationConfiguration::class
notificationManager.createNotificationChannel(channel)
}
//configure base notification
val notification = NotificationCompat.Builder(context, CHANNEL)
val notification = NotificationCompat.Builder(context, notificationConfig.channelId ?: CHANNEL)
.setWhen(System.currentTimeMillis())
.setContentTitle(notificationConfig.title)
.setContentText(notificationConfig.text)
Expand Down

0 comments on commit 8bb3fb1

Please sign in to comment.