Код: Выделить всё
import 'dart:async';
import 'dart:ui';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_background_service_android/flutter_background_service_android.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Future initializeService() async {
final service = FlutterBackgroundService();
AndroidNotificationChannel channel = AndroidNotificationChannel("Reminder", "forground Service",importance: Importance.high );
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin(); await
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation()?.createNotificationChannel(channel);
@pragma('vm-entry point')
void onStart(ServiceInstance service)
{
DartPluginRegistrant.ensureInitialized();
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
if (service is AndroidServiceInstance)
{
service.on('setAsForeground').listen((event) {
service.setAsForegroundService();
});
service.on('setAsBackground').listen((event) {
service.setAsBackgroundService();
});
}
service.on('stopService').listen((event) {
service.stopSelf();
});
Timer.periodic(Duration(seconds: 2), (timer) async {
if(service is AndroidServiceInstance) {
if (await service.isForegroundService()) {
flutterLocalNotificationsPlugin.show(888, "reminder app notification",
"shake feature available", NotificationDetails(
android: AndroidNotificationDetails("Reminder", "forground Service", ongoing: true)
));
}
}
});
}
await service.configure(iosConfiguration: IosConfiguration(), androidConfiguration: AndroidConfiguration(onStart: onStart, isForegroundMode: true
, autoStart: true , notificationChannelId: "Reminder" , initialNotificationTitle: "forground Service" , initialNotificationContent: "initializing"
, foregroundServiceNotificationId: 888));
service.startService();
}
Код: Выделить всё
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeService();
LicenseRegistry.addLicense(() async* {
final license = await rootBundle.loadString('assets/google_fonts/LICENSE.txt');
yield LicenseEntryWithLineBreaks(['google_fonts'], license);
});
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(
statusBarColor: Colors.black,
statusBarIconBrightness: Brightness.light,
));
runApp( MyApp());
}
Подробнее здесь: https://stackoverflow.com/questions/771 ... tatic-func