Skip to content

Commit 1589b62

Browse files
committed
release: 8.16.0
1 parent a5986fe commit 1589b62

File tree

8 files changed

+116
-67
lines changed

8 files changed

+116
-67
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 8.16.0
2+
3+
* [**BREAKING**] Change `ServiceRequestResult` class to `sealed class` for improved code readability
4+
* [**BREAKING**] Change method for customizing notification icon
5+
* [**FEAT**] Add `copyWith` function for models reuse
6+
* Check [migration_documentation](./documentation/migration_documentation.md) for changes
7+
18
## 8.14.0
29

310
* [**FEAT**] Support quickboot for HTC devices

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@ This plugin is used to implement a foreground service on the Android platform.
2121
- Android: `5.0+ (minSdkVersion: 21)`
2222
- iOS: `12.0+`
2323

24-
## Structure
25-
26-
<img src="https://github.com/user-attachments/assets/6fc91bd9-62b2-43b8-9109-26d2e8d809c1" width="800">
27-
2824
## Getting started
2925

3026
To use this plugin, add `flutter_foreground_task` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/). For example:
3127

3228
```yaml
3329
dependencies:
34-
flutter_foreground_task: ^8.14.0
30+
flutter_foreground_task: ^8.16.0
3531
```
3632
3733
After adding the plugin to your flutter project, we need to declare the platform-specific permissions ans service to use for this plugin to work properly.
@@ -204,10 +200,7 @@ class MyTaskHandler extends TaskHandler {
204200
print('onStart(starter: ${starter.name})');
205201
}
206202
207-
// Called by eventAction in [ForegroundTaskOptions].
208-
// - nothing() : Not use onRepeatEvent callback.
209-
// - once() : Call onRepeatEvent only once.
210-
// - repeat(interval) : Call onRepeatEvent at milliseconds interval.
203+
// Called based on the eventAction set in ForegroundTaskOptions.
211204
@override
212205
void onRepeatEvent(DateTime timestamp) {
213206
// Send data to main isolate.
@@ -223,7 +216,7 @@ class MyTaskHandler extends TaskHandler {
223216
print('onDestroy');
224217
}
225218
226-
// Called when data is sent using [FlutterForegroundTask.sendDataToTask].
219+
// Called when data is sent using `FlutterForegroundTask.sendDataToTask`.
227220
@override
228221
void onReceiveData(Object data) {
229222
print('onReceiveData: $data');
@@ -330,9 +323,9 @@ void initState() {
330323
// Add a callback to receive data sent from the TaskHandler.
331324
FlutterForegroundTask.addTaskDataCallback(_onReceiveTaskData);
332325
333-
WidgetsBinding.instance.addPostFrameCallback((_) async {
326+
WidgetsBinding.instance.addPostFrameCallback((_) {
334327
// Request permissions and initialize the service.
335-
await _requestPermissions();
328+
_requestPermissions();
336329
_initService();
337330
});
338331
}
@@ -369,12 +362,18 @@ Future<ServiceRequestResult> _startService() async {
369362
> iOS Platform, `notificationButtons` is not displayed directly in notification.
370363
> When the user slides down the notification, the button is displayed, so you need to guide the user on how to use it.
371364
> https://developer.apple.com/documentation/usernotifications/declaring-your-actionable-notification-types
372-
>
373-
> If you know a better implementation, please let me know on [GitHub](https://github.com/Dev-hwang/flutter_foreground_task/issues) :)
374365
375366
6. Use `FlutterForegroundTask.updateService` to update the service. The options are the same as the start function.
376367

377368
```dart
369+
final ForegroundTaskOptions defaultTaskOptions = ForegroundTaskOptions(
370+
eventAction: ForegroundTaskEventAction.repeat(5000),
371+
autoRunOnBoot: true,
372+
autoRunOnMyPackageReplaced: true,
373+
allowWakeLock: true,
374+
allowWifiLock: true,
375+
);
376+
378377
@pragma('vm:entry-point')
379378
void startCallback() {
380379
FlutterForegroundTask.setTaskHandler(FirstTaskHandler());
@@ -394,7 +393,7 @@ class FirstTaskHandler extends TaskHandler {
394393
395394
if (_count == 10) {
396395
FlutterForegroundTask.updateService(
397-
foregroundTaskOptions: ForegroundTaskOptions(
396+
foregroundTaskOptions: defaultTaskOptions.copyWith(
398397
eventAction: ForegroundTaskEventAction.repeat(1000),
399398
),
400399
callback: updateCallback,
@@ -455,7 +454,7 @@ class SecondTaskHandler extends TaskHandler {
455454
7. If you no longer use the service, call `FlutterForegroundTask.stopService`.
456455

457456
```dart
458-
Future<ServiceRequestResult> _stopService() async {
457+
Future<ServiceRequestResult> _stopService() {
459458
return FlutterForegroundTask.stopService();
460459
}
461460
```

android/src/main/kotlin/com/pravera/flutter_foreground_task/FlutterForegroundTaskLifecycleListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface FlutterForegroundTaskLifecycleListener {
1515
/** Called when the task is started. */
1616
fun onTaskStart(starter: FlutterForegroundTaskStarter)
1717

18-
/** Called by eventAction in ForegroundTaskOptions. */
18+
/** Called based on the eventAction set in ForegroundTaskOptions. */
1919
fun onTaskRepeatEvent()
2020

2121
/** Called when the task is destroyed. */

documentation/migration_documentation.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
11
## Migration
22

3+
### ver 8.16.0
4+
5+
- Change `ServiceRequestResult` class to `sealed class` for improved code readability.
6+
7+
```dart
8+
void startService() {
9+
final ServiceRequestResult result =
10+
await FlutterForegroundTask.startService(
11+
serviceId: 100,
12+
notificationTitle: 'notificationTitle',
13+
notificationText: 'notificationText',
14+
callback: startLocationService,
15+
);
16+
17+
// before: The distinction between success and failure of the request is unclear.
18+
if (result.success) {
19+
// The error should not be accessible when the request is successful.
20+
final Object? error = result.error;
21+
} else {
22+
// Handle error
23+
final Object? error = result.error;
24+
}
25+
26+
// after: The distinction between success and failure of the request is clear.
27+
switch (result) {
28+
case ServiceRequestSuccess():
29+
// The error cannot be accessed.
30+
// final Object error = result.error;
31+
print('success');
32+
case ServiceRequestFailure():
33+
// The error can only be accessed when the request fails,
34+
// and no null check is required.
35+
final Object error = result.error;
36+
print('failure($error)');
37+
}
38+
}
39+
```
40+
41+
- Change method for customizing notification icon. [guide page](./customize_notification_icon.md)
42+
43+
```dart
44+
void startService() {
45+
// before: There was an issue in the foreground service where only the white icon was displayed
46+
// because the icon resource could not be referenced.
47+
await FlutterForegroundTask.startService(
48+
notificationTitle: 'notificationTitle',
49+
notificationText: 'notificationText',
50+
notificationIcon: const NotificationIconData(
51+
resType: ResourceType.drawable,
52+
resPrefix: ResourcePrefix.ic,
53+
name: 'snow',
54+
backgroundColor: Colors.orange,
55+
),
56+
);
57+
58+
// after: Enabled static reference to the icon resource through meta-data.
59+
await FlutterForegroundTask.startService(
60+
notificationTitle: 'notificationTitle',
61+
notificationText: 'notificationText',
62+
notificationIcon: const NotificationIcon(
63+
metaDataName: 'com.your_package.service.SNOW_ICON',
64+
backgroundColor: Colors.orange,
65+
),
66+
);
67+
}
68+
```
69+
370
### ver 8.10.0
471

572
- Change onStart, onDestroy callback return type from `void` to `Future<void>`.

example/lib/main.dart

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ class MyTaskHandler extends TaskHandler {
4141
_incrementCount();
4242
}
4343

44-
// Called by eventAction in [ForegroundTaskOptions].
45-
// - nothing() : Not use onRepeatEvent callback.
46-
// - once() : Call onRepeatEvent only once.
47-
// - repeat(interval) : Call onRepeatEvent at milliseconds interval.
44+
// Called based on the eventAction set in ForegroundTaskOptions.
4845
@override
4946
void onRepeatEvent(DateTime timestamp) {
5047
_incrementCount();
@@ -56,7 +53,7 @@ class MyTaskHandler extends TaskHandler {
5653
print('onDestroy');
5754
}
5855

59-
// Called when data is sent using [FlutterForegroundTask.sendDataToTask].
56+
// Called when data is sent using `FlutterForegroundTask.sendDataToTask`.
6057
@override
6158
void onReceiveData(Object data) {
6259
print('onReceiveData: $data');
@@ -70,25 +67,6 @@ class MyTaskHandler extends TaskHandler {
7067
void onNotificationButtonPressed(String id) {
7168
print('onNotificationButtonPressed: $id');
7269
}
73-
74-
// Called when the notification itself is pressed.
75-
//
76-
// AOS: "android.permission.SYSTEM_ALERT_WINDOW" permission must be granted
77-
// for this function to be called.
78-
@override
79-
void onNotificationPressed() {
80-
FlutterForegroundTask.launchApp('/');
81-
print('onNotificationPressed');
82-
}
83-
84-
// Called when the notification itself is dismissed.
85-
//
86-
// AOS: only work Android 14+
87-
// iOS: only work iOS 10+
88-
@override
89-
void onNotificationDismissed() {
90-
print('onNotificationDismissed');
91-
}
9270
}
9371

9472
class ExampleApp extends StatelessWidget {
@@ -126,19 +104,6 @@ class _ExamplePageState extends State<ExamplePage> {
126104
}
127105

128106
if (Platform.isAndroid) {
129-
// "android.permission.SYSTEM_ALERT_WINDOW" permission must be granted for
130-
// onNotificationPressed function to be called.
131-
//
132-
// When the notification is pressed while permission is denied,
133-
// the onNotificationPressed function is not called and the app opens.
134-
//
135-
// If you do not use the onNotificationPressed or launchApp function,
136-
// you do not need to write this code.
137-
if (!await FlutterForegroundTask.canDrawOverlays) {
138-
// This function requires `android.permission.SYSTEM_ALERT_WINDOW` permission.
139-
await FlutterForegroundTask.openSystemAlertWindowSettings();
140-
}
141-
142107
// Android 12+, there are restrictions on starting a foreground service.
143108
//
144109
// To restart the service on device reboot or unexpected problem, you need to allow below permission.
@@ -202,7 +167,7 @@ class _ExamplePageState extends State<ExamplePage> {
202167
}
203168
}
204169

205-
Future<ServiceRequestResult> _stopService() async {
170+
Future<ServiceRequestResult> _stopService() {
206171
return FlutterForegroundTask.stopService();
207172
}
208173

@@ -221,9 +186,9 @@ class _ExamplePageState extends State<ExamplePage> {
221186
// Add a callback to receive data sent from the TaskHandler.
222187
FlutterForegroundTask.addTaskDataCallback(_onReceiveTaskData);
223188

224-
WidgetsBinding.instance.addPostFrameCallback((_) async {
189+
WidgetsBinding.instance.addPostFrameCallback((_) {
225190
// Request permissions and initialize the service.
226-
await _requestPermissions();
191+
_requestPermissions();
227192
_initService();
228193
});
229194
}

ios/Classes/FlutterForegroundTaskLifecycleListener.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public protocol FlutterForegroundTaskLifecycleListener : AnyObject {
2020
/** Called when the task is started. */
2121
func onTaskStart(starter: FlutterForegroundTaskStarter)
2222

23-
/** Called by eventAction in ForegroundTaskOptions. */
23+
/** Called based on the eventAction set in ForegroundTaskOptions. */
2424
func onTaskRepeatEvent()
2525

2626
/** Called when the task is destroyed. */

lib/task_handler.dart

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ abstract class TaskHandler {
55
/// Called when the task is started.
66
Future<void> onStart(DateTime timestamp, TaskStarter starter);
77

8-
/// Called by eventAction in [ForegroundTaskOptions].
8+
/// Called based on the eventAction set in [ForegroundTaskOptions].
99
///
10-
/// - nothing() : Not use onRepeatEvent callback.
11-
/// - once() : Call onRepeatEvent only once.
12-
/// - repeat(interval) : Call onRepeatEvent at milliseconds interval.
10+
/// - .nothing() : Not use onRepeatEvent callback.
11+
/// - .once() : Call onRepeatEvent only once.
12+
/// - .repeat(interval) : Call onRepeatEvent at milliseconds interval.
1313
void onRepeatEvent(DateTime timestamp);
1414

1515
/// Called when the task is destroyed.
@@ -23,14 +23,25 @@ abstract class TaskHandler {
2323

2424
/// Called when the notification itself is pressed.
2525
///
26-
/// AOS: "android.permission.SYSTEM_ALERT_WINDOW" permission must be granted
27-
/// for this function to be called.
26+
/// - AOS: This callback is triggered only if the
27+
/// "android.permission.SYSTEM_ALERT_WINDOW" permission is declared and granted.
28+
///
29+
/// ```dart
30+
/// void requestPermission() async {
31+
/// if (!await FlutterForegroundTask.canDrawOverlays) {
32+
/// await FlutterForegroundTask.openSystemAlertWindowSettings();
33+
/// }
34+
/// }
35+
/// ```
36+
///
37+
/// - iOS: only work iOS 12+
2838
void onNotificationPressed() => FlutterForegroundTask.launchApp();
2939

3040
/// Called when the notification itself is dismissed.
3141
///
32-
/// AOS: only work Android 14+
33-
/// iOS: only work iOS 10+
42+
/// - AOS: only work Android 14+
43+
///
44+
/// - iOS: only work iOS 12+
3445
void onNotificationDismissed() {}
3546
}
3647

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_foreground_task
22
description: This plugin is used to implement a foreground service on the Android platform.
3-
version: 8.14.0
3+
version: 8.16.0
44
homepage: https://github.com/Dev-hwang/flutter_foreground_task
55

66
environment:

0 commit comments

Comments
 (0)