From f29d2af1ae5f14aa9c92543cacd3d8a5346294ae Mon Sep 17 00:00:00 2001 From: Jonah Hirsch Date: Sun, 12 Sep 2021 17:47:39 -0700 Subject: [PATCH] Add ability to fully customize the Foreground Service notification Introduces two new optional parameters to NotificationProperties' default constructor: * notificationChannelId: This allows developers to customize the Notification Channel ID, so they can put the notification in the exact channel they desire * notificationBuilderExtender: Accepts a lambda that can modify the Notification.Builder so developers can fully customize the Foreground Service notification --- .../runner/TaskerPluginRunner.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/taskerpluginlibrary/src/main/java/com/joaomgcd/taskerpluginlibrary/runner/TaskerPluginRunner.kt b/taskerpluginlibrary/src/main/java/com/joaomgcd/taskerpluginlibrary/runner/TaskerPluginRunner.kt index 39a2af8..f3f9c12 100644 --- a/taskerpluginlibrary/src/main/java/com/joaomgcd/taskerpluginlibrary/runner/TaskerPluginRunner.kt +++ b/taskerpluginlibrary/src/main/java/com/joaomgcd/taskerpluginlibrary/runner/TaskerPluginRunner.kt @@ -28,12 +28,15 @@ abstract class TaskerPluginRunner { val notificationChannelDescriptionResId: Int = R.string.tasker_plugin_service_description, val titleResId: Int = R.string.app_name, val textResId: Int = R.string.running_tasker_plugin, - val iconResId: Int = R.mipmap.ic_launcher) { + val iconResId: Int = R.mipmap.ic_launcher, + val notificationChannelId: String = NOTIFICATION_CHANNEL_ID, + val notificationBuilderExtender: Notification.Builder.(context: Context) -> Notification.Builder = {this}) { @TargetApi(Build.VERSION_CODES.O) - fun getNotification(context: Context) = Notification.Builder(context, NOTIFICATION_CHANNEL_ID) + fun getNotification(context: Context) = Notification.Builder(context, notificationChannelId) .setContentTitle(context.getString(titleResId)) .setContentText(context.getString(textResId)) .setSmallIcon(Icon.createWithResource(context, iconResId)) + .notificationBuilderExtender(context) .build() } @@ -49,11 +52,11 @@ abstract class TaskerPluginRunner { companion object { - const val NOTIFICATION_CHANNEL_ID = "taskerpluginforegroundd" + private const val NOTIFICATION_CHANNEL_ID = "taskerpluginforegroundd" @TargetApi(Build.VERSION_CODES.O) - fun Service.createNotificationChannel(channelId: String, notificationProperties: NotificationProperties) { + fun Service.createNotificationChannel(notificationProperties: NotificationProperties) { val notificationManager = getSystemService(NotificationManager::class.java) - val channel = NotificationChannel(channelId, getString(notificationProperties.notificationChannelNameResId), NotificationManager.IMPORTANCE_NONE) + val channel = NotificationChannel(notificationProperties.notificationChannelId, getString(notificationProperties.notificationChannelNameResId), NotificationManager.IMPORTANCE_NONE) channel.description = getString(notificationProperties.notificationChannelDescriptionResId) notificationManager.createNotificationChannel(channel) } @@ -64,8 +67,7 @@ abstract class TaskerPluginRunner { @TargetApi(Build.VERSION_CODES.O) fun startForegroundIfNeeded(intentService: Service, notificationProperties: NotificationProperties = NotificationProperties()) { if (!intentService.hasToRunServicesInForeground) return - val channelId = NOTIFICATION_CHANNEL_ID - intentService.createNotificationChannel(channelId, notificationProperties) + intentService.createNotificationChannel(notificationProperties) val notification: Notification = notificationProperties.getNotification(intentService) intentService.startForeground(this.hashCode(), notification) }