From 2e75a16831ee79d83936cb8b96d501e7247ea47c Mon Sep 17 00:00:00 2001 From: Ross Savage Date: Wed, 27 Mar 2024 14:20:04 +0100 Subject: [PATCH 1/2] Give protected access to the logger and add functions to create custom notification channels --- .../ForegroundService.kt | 2 +- .../NotificationHelper.kt | 71 ++++++++++++++++--- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/ForegroundService.kt b/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/ForegroundService.kt index eedd96da7..d661d3fd0 100644 --- a/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/ForegroundService.kt +++ b/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/ForegroundService.kt @@ -31,7 +31,7 @@ abstract class ForegroundService : SdkForegroundService, 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() companion object { diff --git a/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/NotificationHelper.kt b/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/NotificationHelper.kt index cceb6da16..7b00438ad 100644 --- a/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/NotificationHelper.kt +++ b/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/NotificationHelper.kt @@ -71,22 +71,75 @@ class NotificationHelper { companion object { 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 } - Log.d(TAG, "Registered notification channels") } + return null } + @RequiresApi(Build.VERSION_CODES.O) + 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) + } + } + + @RequiresApi(Build.VERSION_CODES.O) + 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) + } + } + + @RequiresApi(Build.VERSION_CODES.O) + 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") + } + } + @RequiresApi(Build.VERSION_CODES.O) private fun createNotificationChannels( context: Context, @@ -165,9 +218,9 @@ class NotificationHelper { ) ) } - + @RequiresApi(Build.VERSION_CODES.O) - private fun createNotificationChannelGroup( + private fun createNotificationChannelGroups( context: Context, notificationManager: NotificationManager, ) { From 094968c4f0d030e442591626de22894af1f85b70 Mon Sep 17 00:00:00 2001 From: Ross Savage Date: Wed, 27 Mar 2024 14:36:28 +0100 Subject: [PATCH 2/2] Use NewApi to suppress lint as getNotificationManager checks the version --- .../NotificationHelper.kt | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/NotificationHelper.kt b/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/NotificationHelper.kt index 7b00438ad..72d66e728 100644 --- a/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/NotificationHelper.kt +++ b/libs/sdk-bindings/bindings-android/lib/src/main/kotlin/breez_sdk_notification/NotificationHelper.kt @@ -71,7 +71,7 @@ class NotificationHelper { companion object { private const val TAG = "NotificationHelper" private var defaultClickAction: String? = null - + private fun getNotificationManager(context: Context): NotificationManager? { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val notificationManager = @@ -84,7 +84,7 @@ class NotificationHelper { return null } - @RequiresApi(Build.VERSION_CODES.O) + @SuppressLint("NewApi") private fun createNotificationChannelGroup( context: Context, groupId: String, @@ -104,14 +104,14 @@ class NotificationHelper { manager.createNotificationChannelGroup(channelGroup) } } - - @RequiresApi(Build.VERSION_CODES.O) + + @SuppressLint("NewApi") fun createNotificationChannel( - context: Context, - channelId: String, - channelName: String, - channelDescription: String, - groupId: String, + context: Context, + channelId: String, + channelName: String, + channelDescription: String, + groupId: String, importance: Int = NotificationManager.IMPORTANCE_DEFAULT, ) { getNotificationManager(context)?.also { manager -> @@ -129,7 +129,7 @@ class NotificationHelper { } } - @RequiresApi(Build.VERSION_CODES.O) + @SuppressLint("NewApi") fun registerNotificationChannels(context: Context, defaultClickAction: String? = null) { this.defaultClickAction = defaultClickAction @@ -139,7 +139,7 @@ class NotificationHelper { Log.d(TAG, "Registered notification channels") } } - + @RequiresApi(Build.VERSION_CODES.O) private fun createNotificationChannels( context: Context, @@ -218,7 +218,7 @@ class NotificationHelper { ) ) } - + @RequiresApi(Build.VERSION_CODES.O) private fun createNotificationChannelGroups( context: Context,