@@ -5,7 +5,6 @@ import android.app.NotificationManager
55import android.content.Context
66import android.os.Build
77import androidx.core.app.NotificationCompat
8- import androidx.work.ListenableWorker
98import java.text.DateFormat
109import java.util.Date
1110import java.util.concurrent.TimeUnit.MILLISECONDS
@@ -14,7 +13,7 @@ import kotlin.random.Random
1413/* *
1514 * A debug handler that shows notifications for task events.
1615 * Use this to see task execution as notifications on the device.
17- *
16+ *
1817 * Note: You need to ensure your app has notification permissions.
1918 */
2019class NotificationDebugHandler : WorkmanagerDebugHandler {
@@ -30,7 +29,10 @@ class NotificationDebugHandler : WorkmanagerDebugHandler {
3029 private val failureEmoji = " 🔥"
3130 private val currentTime get() = debugDateFormatter.format(Date ())
3231
33- override fun onTaskStarting (context : Context , taskInfo : TaskDebugInfo ) {
32+ override fun onTaskStarting (
33+ context : Context ,
34+ taskInfo : TaskDebugInfo ,
35+ ) {
3436 val notificationId = Random .nextInt()
3537 postNotification(
3638 context,
@@ -40,16 +42,20 @@ class NotificationDebugHandler : WorkmanagerDebugHandler {
4042 • Task Starting: ${taskInfo.taskName}
4143 • Input Data: ${taskInfo.inputData ? : " none" }
4244 • Callback Handle: ${taskInfo.callbackHandle}
43- """ .trimIndent()
45+ """ .trimIndent(),
4446 )
4547 }
4648
47- override fun onTaskCompleted (context : Context , taskInfo : TaskDebugInfo , result : TaskResult ) {
49+ override fun onTaskCompleted (
50+ context : Context ,
51+ taskInfo : TaskDebugInfo ,
52+ result : TaskResult ,
53+ ) {
4854 val notificationId = Random .nextInt()
4955 val emoji = if (result.success) successEmoji else failureEmoji
5056 val status = if (result.success) " SUCCESS" else " FAILURE"
5157 val duration = MILLISECONDS .toSeconds(result.duration)
52-
58+
5359 postNotification(
5460 context,
5561 notificationId,
@@ -60,7 +66,7 @@ class NotificationDebugHandler : WorkmanagerDebugHandler {
6066 • Input Data: ${taskInfo.inputData ? : " none" }
6167 • Duration: ${duration} s
6268 ${if (result.error != null ) " • Error: ${result.error} " else " " }
63- """ .trimIndent()
69+ """ .trimIndent(),
6470 )
6571 }
6672
@@ -71,33 +77,34 @@ class NotificationDebugHandler : WorkmanagerDebugHandler {
7177 contentText : String ,
7278 ) {
7379 val notificationManager = context.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
74-
80+
7581 createNotificationChannel(notificationManager)
76-
77- val notification = NotificationCompat
78- . Builder (context, DEBUG_CHANNEL_ID )
79- .setContentTitle(title )
80- .setContentText(contentText )
81- .setStyle(
82- NotificationCompat
83- . BigTextStyle ()
84- .bigText(contentText )
85- )
86- .setSmallIcon(android.R .drawable.stat_notify_sync)
87- .setPriority(NotificationCompat .PRIORITY_LOW )
88- .build()
89-
82+
83+ val notification =
84+ NotificationCompat
85+ . Builder (context, DEBUG_CHANNEL_ID )
86+ .setContentTitle(title )
87+ .setContentText(contentText)
88+ .setStyle(
89+ NotificationCompat
90+ . BigTextStyle ( )
91+ .bigText(contentText),
92+ ) .setSmallIcon(android.R .drawable.stat_notify_sync)
93+ .setPriority(NotificationCompat .PRIORITY_LOW )
94+ .build()
95+
9096 notificationManager.notify(notificationId, notification)
9197 }
9298
9399 private fun createNotificationChannel (notificationManager : NotificationManager ) {
94100 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
95- val channel = NotificationChannel (
96- DEBUG_CHANNEL_ID ,
97- DEBUG_CHANNEL_NAME ,
98- NotificationManager .IMPORTANCE_LOW
99- )
101+ val channel =
102+ NotificationChannel (
103+ DEBUG_CHANNEL_ID ,
104+ DEBUG_CHANNEL_NAME ,
105+ NotificationManager .IMPORTANCE_LOW ,
106+ )
100107 notificationManager.createNotificationChannel(channel)
101108 }
102109 }
103- }
110+ }
0 commit comments