Skip to content

Commit e707f26

Browse files
enedclaude
andcommitted
feat: enhance debug system with task status tracking and configurable notifications
- Add TaskStatus.SCHEDULED notifications when tasks are registered - Add TaskStatus.RESCHEDULED status to distinguish from RETRYING - Fix notification flow: Started → Rescheduled → Retrying → Success - Add configurable notification channels and grouping for debug handlers - Update notification icons to cleaner symbols (▶️ ✅ ❌ 🔄 ⏹️ 📅) - Add comprehensive task status documentation with platform differences - Fix Android retry detection using runAttemptCount - Remove duplicate exception notifications for normal task failures - Update example apps to demonstrate new debug configuration 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 44be9a8 commit e707f26

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

example/android/app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
22

3+
<!-- Required for notification debug handler -->
4+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
5+
36
<application
7+
android:name="dev.fluttercommunity.workmanager_example.ExampleApplication"
48
android:icon="@mipmap/ic_launcher"
59
android:label="workmanager_example">
610
<activity
7-
android:name="io.flutter.embedding.android.FlutterActivity"
11+
android:name="dev.fluttercommunity.workmanager_example.MainActivity"
812
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
913
android:hardwareAccelerated="true"
1014
android:launchMode="singleTop"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package dev.fluttercommunity.workmanager_example
2+
3+
import android.app.NotificationChannel
4+
import android.app.NotificationManager
5+
import android.os.Build
6+
import dev.fluttercommunity.workmanager.LoggingDebugHandler
7+
import dev.fluttercommunity.workmanager.NotificationDebugHandler
8+
import dev.fluttercommunity.workmanager.WorkmanagerDebug
9+
import io.flutter.app.FlutterApplication
10+
11+
class ExampleApplication : FlutterApplication() {
12+
13+
override fun onCreate() {
14+
super.onCreate()
15+
16+
// Create custom notification channel for debug notifications
17+
val debugChannelId = "workmanager_debug"
18+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
19+
val channel = NotificationChannel(
20+
debugChannelId,
21+
"Workmanager Example Debug",
22+
NotificationManager.IMPORTANCE_DEFAULT
23+
).apply {
24+
description = "Debug notifications for background tasks in example app"
25+
}
26+
27+
val notificationManager = getSystemService(NotificationManager::class.java)
28+
notificationManager.createNotificationChannel(channel)
29+
}
30+
31+
// EXAMPLE: Enable debug handlers for background tasks
32+
// Choose one of the following options:
33+
34+
// Option 1: Custom notification handler using our custom channel
35+
WorkmanagerDebug.setCurrent(NotificationDebugHandler(
36+
channelId = debugChannelId,
37+
channelName = "Workmanager Example Debug",
38+
groupKey = "workmanager_example_group"
39+
))
40+
41+
// Option 2: Default notification handler (creates and uses default channel)
42+
// WorkmanagerDebug.setCurrent(NotificationDebugHandler())
43+
44+
// Option 3: Logging-based debug handler (writes to system log)
45+
// WorkmanagerDebug.setCurrent(LoggingDebugHandler())
46+
47+
// Note: For Android 13+, the app needs to request POST_NOTIFICATIONS permission
48+
// at runtime from the Flutter side or in the first activity
49+
}
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dev.fluttercommunity.workmanager_example
2+
3+
import android.Manifest
4+
import android.content.pm.PackageManager
5+
import android.os.Build
6+
import androidx.core.app.ActivityCompat
7+
import androidx.core.content.ContextCompat
8+
import io.flutter.embedding.android.FlutterActivity
9+
10+
class MainActivity : FlutterActivity() {
11+
companion object {
12+
private const val NOTIFICATION_PERMISSION_REQUEST_CODE = 1001
13+
}
14+
15+
override fun onStart() {
16+
super.onStart()
17+
18+
// Request notification permission for Android 13+ (API 33+)
19+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
20+
if (ContextCompat.checkSelfPermission(
21+
this,
22+
Manifest.permission.POST_NOTIFICATIONS
23+
) != PackageManager.PERMISSION_GRANTED
24+
) {
25+
ActivityCompat.requestPermissions(
26+
this,
27+
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
28+
NOTIFICATION_PERMISSION_REQUEST_CODE
29+
)
30+
}
31+
}
32+
}
33+
34+
override fun onRequestPermissionsResult(
35+
requestCode: Int,
36+
permissions: Array<out String>,
37+
grantResults: IntArray
38+
) {
39+
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
40+
41+
if (requestCode == NOTIFICATION_PERMISSION_REQUEST_CODE) {
42+
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
43+
// Permission granted - debug notifications will work
44+
println("Notification permission granted for debug handler")
45+
} else {
46+
// Permission denied - debug notifications won't show
47+
println("Notification permission denied - debug notifications will not be shown")
48+
}
49+
}
50+
}
51+
}

example/ios/Runner/AppDelegate.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ import workmanager_apple
1313
GeneratedPluginRegistrant.register(with: self)
1414
UNUserNotificationCenter.current().delegate = self
1515

16+
// Request notification permission for debug handler
17+
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
18+
if granted {
19+
print("Notification permission granted for debug handler")
20+
} else if let error = error {
21+
print("Error requesting notification permission: \(error)")
22+
}
23+
}
24+
25+
// EXAMPLE: Enable debug notifications for background tasks
26+
// Uncomment one of the following lines to enable debug output:
27+
28+
// Option 1: Notification-based debug handler (shows debug info as notifications)
29+
WorkmanagerDebug.setCurrent(NotificationDebugHandler())
30+
31+
// Option 2: Logging-based debug handler (writes to system log)
32+
// WorkmanagerDebug.setCurrent(LoggingDebugHandler())
33+
1634
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
1735
// Registry in this case is the FlutterEngine that is created in Workmanager's
1836
// performFetchWithCompletionHandler or BGAppRefreshTask.

0 commit comments

Comments
 (0)