flutter_foreground_task 3.5.3 flutter_foreground_task: ^3.5.3 copied to clipboard
This plugin is used to implement a foreground service on the Android platform.
import 'dart:isolate';
import 'package:flutter/material.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
void main() => runApp(const ExampleApp());
// The callback function should always be a top-level function.
void startCallback() {
// The setTaskHandler function must be called to handle the task in the background.
FlutterForegroundTask.setTaskHandler(FirstTaskHandler());
}
class FirstTaskHandler extends TaskHandler {
int updateCount = 0;
@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
// You can use the getData function to get the data you saved.
final customData =
await FlutterForegroundTask.getData<String>(key: 'customData');
print('customData: $customData');
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
FlutterForegroundTask.updateService(
notificationTitle: 'FirstTaskHandler',
notificationText: timestamp.toString(),
callback: updateCount >= 10 ? updateCallback : null);
// Send data to the main isolate.
sendPort?.send(timestamp);
sendPort?.send(updateCount);
updateCount++;
}
@override
Future<void> onDestroy(DateTime timestamp) async {
// You can use the clearAllData function to clear all the stored data.
await FlutterForegroundTask.clearAllData();
}
@override
void onButtonPressed(String id) {
// Called when the notification button on the Android platform is pressed.
print('onButtonPressed >> $id');
}
}
void updateCallback() {
FlutterForegroundTask.setTaskHandler(SecondTaskHandler());
}
class SecondTaskHandler extends TaskHandler {
@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
FlutterForegroundTask.updateService(
notificationTitle: 'SecondTaskHandler',
notificationText: timestamp.toString());
// Send data to the main isolate.
sendPort?.send(timestamp);
}
@override
Future<void> onDestroy(DateTime timestamp) async {
}
}
class ExampleApp extends StatefulWidget {
const ExampleApp({Key? key}) : super(key: key);
@override
_ExampleAppState createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
ReceivePort? _receivePort;
Future<void> _initForegroundTask() async {
await FlutterForegroundTask.init(
androidNotificationOptions: AndroidNotificationOptions(
channelId: 'notification_channel_id',
channelName: 'Foreground Notification',
channelDescription:
'This notification appears when the foreground service is running.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
iconData: const NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
buttons: [
const NotificationButton(id: 'sendButton', text: 'Send'),
const NotificationButton(id: 'testButton', text: 'Test'),
],
),
iosNotificationOptions: const IOSNotificationOptions(
showNotification: true,
playSound: false,
),
foregroundTaskOptions: const ForegroundTaskOptions(
interval: 5000,
autoRunOnBoot: true,
allowWifiLock: true,
),
printDevLog: true,
);
}
Future<bool> _startForegroundTask() async {
// You can save data using the saveData function.
await FlutterForegroundTask.saveData(key: 'customData', value: 'hello');
ReceivePort? receivePort;
if (await FlutterForegroundTask.isRunningService) {
receivePort = await FlutterForegroundTask.restartService();
} else {
receivePort = await FlutterForegroundTask.startService(
notificationTitle: 'Foreground Service is running',
notificationText: 'Tap to return to the app',
callback: startCallback,
);
}
if (receivePort != null) {
_receivePort = receivePort;
_receivePort?.listen((message) {
if (message is DateTime) {
print('receive timestamp: $message');
} else if (message is int) {
print('receive updateCount: $message');
}
});
return true;
}
return false;
}
Future<bool> _stopForegroundTask() async {
return await FlutterForegroundTask.stopService();
}
@override
void initState() {
super.initState();
_initForegroundTask();
}
@override
void dispose() {
_receivePort?.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
// A widget that prevents the app from closing when the foreground service is running.
// This widget must be declared above the [Scaffold] widget.
home: WithForegroundTask(
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter Foreground Task'),
centerTitle: true,
),
body: _buildContentView(),
),
),
);
}
Widget _buildContentView() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildTestButton('start', onPressed: _startForegroundTask),
_buildTestButton('stop', onPressed: _stopForegroundTask),
],
),
);
}
Widget _buildTestButton(String text, {VoidCallback? onPressed}) {
return ElevatedButton(
child: Text(text),
onPressed: onPressed,
);
}
}