Skip to content

Commit

Permalink
docs: Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-hwang committed Oct 29, 2024
1 parent 532a76c commit bf6721d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 64 deletions.
52 changes: 11 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ Open the `ios/Runner/AppDelegate.swift` file and add the commented code.
#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"

// here
// this
#import <flutter_foreground_task/FlutterForegroundTaskPlugin.h>

// here
// this
void registerPlugins(NSObject<FlutterPluginRegistry>* registry) {
[GeneratedPluginRegistrant registerWithRegistry:registry];
}
Expand All @@ -127,7 +127,7 @@ void registerPlugins(NSObject<FlutterPluginRegistry>* registry) {
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];

// here
// this
[FlutterForegroundTaskPlugin setPluginRegistrantCallback:registerPlugins];
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
Expand All @@ -153,15 +153,15 @@ Open the `ios/Runner/AppDelegate.swift` file and add the commented code.
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)

// here
// this
SwiftFlutterForegroundTaskPlugin.setPluginRegistrantCallback { registry in
GeneratedPluginRegistrant.register(with: registry)
}
Expand Down Expand Up @@ -234,25 +234,6 @@ class MyTaskHandler extends TaskHandler {
void onNotificationButtonPressed(String id) {
print('onNotificationButtonPressed: $id');
}
// Called when the notification itself is pressed.
//
// AOS: "android.permission.SYSTEM_ALERT_WINDOW" permission must be granted
// for this function to be called.
@override
void onNotificationPressed() {
FlutterForegroundTask.launchApp('/');
print('onNotificationPressed');
}
// Called when the notification itself is dismissed.
//
// AOS: only work Android 14+
// iOS: only work iOS 10+
@override
void onNotificationDismissed() {
print('onNotificationDismissed');
}
}
```

Expand Down Expand Up @@ -299,19 +280,6 @@ Future<void> _requestPermissions() async {
}
if (Platform.isAndroid) {
// "android.permission.SYSTEM_ALERT_WINDOW" permission must be granted for
// onNotificationPressed function to be called.
//
// When the notification is pressed while permission is denied,
// the onNotificationPressed function is not called and the app opens.
//
// If you do not use the onNotificationPressed or launchApp function,
// you do not need to write this code.
if (!await FlutterForegroundTask.canDrawOverlays) {
// This function requires `android.permission.SYSTEM_ALERT_WINDOW` permission.
await FlutterForegroundTask.openSystemAlertWindowSettings();
}
// Android 12+, there are restrictions on starting a foreground service.
//
// To restart the service on device reboot or unexpected problem, you need to allow below permission.
Expand Down Expand Up @@ -340,6 +308,7 @@ void _initService() {
channelName: 'Foreground Service Notification',
channelDescription:
'This notification appears when the foreground service is running.',
onlyAlertOnce: true,
),
iosNotificationOptions: const IOSNotificationOptions(
showNotification: false,
Expand All @@ -361,9 +330,9 @@ void initState() {
// Add a callback to receive data sent from the TaskHandler.
FlutterForegroundTask.addTaskDataCallback(_onReceiveTaskData);
WidgetsBinding.instance.addPostFrameCallback((_) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
// Request permissions and initialize the service.
_requestPermissions();
await _requestPermissions();
_initService();
});
}
Expand Down Expand Up @@ -432,7 +401,7 @@ class FirstTaskHandler extends TaskHandler {
);
} else {
FlutterForegroundTask.updateService(
notificationTitle: 'FirstTask',
notificationTitle: 'Hello FirstTaskHandler :)',
notificationText: timestamp.toString(),
);
Expand Down Expand Up @@ -464,7 +433,7 @@ class SecondTaskHandler extends TaskHandler {
@override
void onRepeatEvent(DateTime timestamp) {
FlutterForegroundTask.updateService(
notificationTitle: 'SecondTask',
notificationTitle: 'Hello SecondTaskHandler :)',
notificationText: timestamp.toString(),
);
Expand Down Expand Up @@ -583,6 +552,7 @@ class MyTaskHandler extends TaskHandler {
* [`internal_plugin_service`](https://github.com/Dev-hwang/flutter_foreground_task_example/tree/main/internal_plugin_service) (Recommend)
* [`location_service`](https://github.com/Dev-hwang/flutter_foreground_task_example/tree/main/location_service)
* [`record_service`](https://github.com/Dev-hwang/flutter_foreground_task_example/tree/main/record_service)
* [`geofencing_service`](https://github.com/Dev-hwang/flutter_foreground_task_example/tree/main/geofencing_service)

## More Documentation

Expand Down
1 change: 1 addition & 0 deletions documentation/models_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Notification options for Android platform.
| `playSound` | Whether to play sound when creating notifications. The default is `false`. It is set only once for the first time on Android 8.0+. |
| `showWhen` | Whether to show the timestamp when the notification was created in the content view. The default is `false`. |
| `showBadge` | Whether to show the badge near the app icon when service is started. The default is `false`. It is set only once for the first time on Android 8.0+. |
| `onlyAlertOnce` | Whether to only alert once when the notification is created. The default is `false`. |
| `visibility` | Control the level of detail displayed in notifications on the lock screen. The default is `NotificationVisibility.VISIBILITY_PUBLIC`. |

### :chicken: IOSNotificationOptions
Expand Down
10 changes: 4 additions & 6 deletions example/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)

SwiftFlutterForegroundTaskPlugin.setPluginRegistrantCallback(registerPlugins)
SwiftFlutterForegroundTaskPlugin.setPluginRegistrantCallback { registry in
GeneratedPluginRegistrant.register(with: registry)
}
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
}

return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

func registerPlugins(registry: FlutterPluginRegistry) {
GeneratedPluginRegistrant.register(with: registry)
}
41 changes: 24 additions & 17 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ExamplePage extends StatefulWidget {
}

class _ExamplePageState extends State<ExamplePage> {
final ValueNotifier<Object?> _receivedTaskData = ValueNotifier(null);
final ValueNotifier<Object?> _taskDataListenable = ValueNotifier(null);

Future<void> _requestPermissions() async {
// Android 13+, you need to allow notification permission to display foreground service notification.
Expand Down Expand Up @@ -167,6 +167,7 @@ class _ExamplePageState extends State<ExamplePage> {
channelName: 'Foreground Service Notification',
channelDescription:
'This notification appears when the foreground service is running.',
onlyAlertOnce: true,
),
iosNotificationOptions: const IOSNotificationOptions(
showNotification: false,
Expand Down Expand Up @@ -205,7 +206,7 @@ class _ExamplePageState extends State<ExamplePage> {

void _onReceiveTaskData(Object data) {
print('onReceiveTaskData: $data');
_receivedTaskData.value = data;
_taskDataListenable.value = data;
}

void _incrementCount() {
Expand All @@ -218,9 +219,9 @@ class _ExamplePageState extends State<ExamplePage> {
// Add a callback to receive data sent from the TaskHandler.
FlutterForegroundTask.addTaskDataCallback(_onReceiveTaskData);

WidgetsBinding.instance.addPostFrameCallback((_) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
// Request permissions and initialize the service.
_requestPermissions();
await _requestPermissions();
_initService();
});
}
Expand All @@ -229,7 +230,7 @@ class _ExamplePageState extends State<ExamplePage> {
void dispose() {
// Remove a callback to receive data sent from the TaskHandler.
FlutterForegroundTask.removeTaskDataCallback(_onReceiveTaskData);
_receivedTaskData.dispose();
_taskDataListenable.dispose();
super.dispose();
}

Expand All @@ -242,27 +243,33 @@ class _ExamplePageState extends State<ExamplePage> {
// This widget must be declared above the [Scaffold] widget.
return WithForegroundTask(
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter Foreground Task'),
centerTitle: true,
),
body: _buildContentView(),
appBar: _buildAppBar(),
body: _buildContent(),
),
);
}

Widget _buildContentView() {
return Column(
children: [
Expanded(child: _buildCommunicationText()),
_buildServiceControlButtons(),
],
AppBar _buildAppBar() {
return AppBar(
title: const Text('Flutter Foreground Task'),
centerTitle: true,
);
}

Widget _buildContent() {
return SafeArea(
child: Column(
children: [
Expanded(child: _buildCommunicationText()),
_buildServiceControlButtons(),
],
),
);
}

Widget _buildCommunicationText() {
return ValueListenableBuilder(
valueListenable: _receivedTaskData,
valueListenable: _taskDataListenable,
builder: (context, data, _) {
return Center(
child: Column(
Expand Down

0 comments on commit bf6721d

Please sign in to comment.