Skip to content

Commit

Permalink
Merge pull request #899 from breez/savage-notification-plugin-improve…
Browse files Browse the repository at this point in the history
…ments

Android Notification Plugin customisation
  • Loading branch information
dangeross authored Apr 11, 2024
2 parents 068d4a8 + 094968c commit 9a36aac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class ForegroundService : SdkForegroundService, EventListener, Service(
private var breezSDK: BlockingBreezServices? = null
@Suppress("MemberVisibilityCanBePrivate")
val serviceScope = CoroutineScope(Dispatchers.Main.immediate + SupervisorJob())
private var logger: ServiceLogger = ServiceLogger()
protected var logger: ServiceLogger = ServiceLogger()
private var config: ServiceConfig = ServiceConfig.default()
private var jobs: MutableList<Job> = arrayListOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,70 @@ class NotificationHelper {
private const val TAG = "NotificationHelper"
private var defaultClickAction: String? = null

fun registerNotificationChannels(context: Context, defaultClickAction: String? = null) {
this.defaultClickAction = defaultClickAction

private fun getNotificationManager(context: Context): NotificationManager? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE)
as NotificationManager
if (notificationManager.areNotificationsEnabled()) {
createNotificationChannelGroup(context, notificationManager)
createNotificationChannels(context, notificationManager)
return notificationManager
}
}
return null
}

@SuppressLint("NewApi")
private fun createNotificationChannelGroup(
context: Context,
groupId: String,
groupName: String,
groupDescription: String,
) {
getNotificationManager(context)?.also { manager ->
val channelGroup = NotificationChannelGroup(
groupId,
groupName,
)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
channelGroup.description = groupDescription
}

manager.createNotificationChannelGroup(channelGroup)
}
}

@SuppressLint("NewApi")
fun createNotificationChannel(
context: Context,
channelId: String,
channelName: String,
channelDescription: String,
groupId: String,
importance: Int = NotificationManager.IMPORTANCE_DEFAULT,
) {
getNotificationManager(context)?.also { manager ->
val applicationId = context.applicationContext.packageName
val notificationChannel = NotificationChannel(
"${applicationId}.${channelId}",
channelName,
importance
).apply {
description = channelDescription
group = groupId
}

manager.createNotificationChannel(notificationChannel)
}
}

@SuppressLint("NewApi")
fun registerNotificationChannels(context: Context, defaultClickAction: String? = null) {
this.defaultClickAction = defaultClickAction

getNotificationManager(context)?.also { manager ->
createNotificationChannelGroups(context, manager)
createNotificationChannels(context, manager)
Log.d(TAG, "Registered notification channels")
}
}
Expand Down Expand Up @@ -167,7 +220,7 @@ class NotificationHelper {
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannelGroup(
private fun createNotificationChannelGroups(
context: Context,
notificationManager: NotificationManager,
) {
Expand Down

0 comments on commit 9a36aac

Please sign in to comment.