diff --git a/lib/src/background_refresh/background_refresh_manager.dart b/lib/src/background_refresh/background_refresh_manager.dart index 196b2fd0..1e8d0fd2 100644 --- a/lib/src/background_refresh/background_refresh_manager.dart +++ b/lib/src/background_refresh/background_refresh_manager.dart @@ -46,27 +46,30 @@ import 'package:logging/logging.dart'; import 'package:ox_coi/src/notifications/local_notification_manager.dart'; import 'package:ox_coi/src/utils/constants.dart'; -void backgroundHeadlessTask() async { +void backgroundHeadlessTask(String taskId) async { + final _logger = Logger("background_refresh_manager");// TODO not logging cause no global logger configured + _logger.info("Callback (background) triggered"); var core = DeltaChatCore(); - var init = await core.init(dbName); - if (init) { - await core.start(); + var isSetup = await core.setupAsync(dbName); + if (isSetup) { + _logger.info("Callback (background) checking for new messages"); await getMessages(); - await core.stop(); + await core.tearDownAsync(); } - BackgroundFetch.finish(); + _logger.info("Callback (background) finishing"); + BackgroundFetch.finish(taskId); } Future getMessages() async { - var context = Context(); + final context = Context(); await context.interruptIdleForIncomingMessages(); - var localNotificationManager = LocalNotificationManager(); + final localNotificationManager = LocalNotificationManager.recreate(); localNotificationManager.setup(); await localNotificationManager.triggerNotificationAsync(); } class BackgroundRefreshManager { - final Logger _logger = Logger("background_refresh_manager"); + final _logger = Logger("background_refresh_manager"); static BackgroundRefreshManager _instance; @@ -77,22 +80,27 @@ class BackgroundRefreshManager { BackgroundRefreshManager._internal(); setupAndStart() { - BackgroundFetch.registerHeadlessTask(backgroundHeadlessTask); + BackgroundFetch.registerHeadlessTask(backgroundHeadlessTask).then((value) { + _logger.info("Register headless task"); + }); BackgroundFetch.configure( - BackgroundFetchConfig( - minimumFetchInterval: 15, - stopOnTerminate: false, - enableHeadless: true, - startOnBoot: true, - ), - _callback); - _running = true; - _logger.info("Configured and started background fetch"); - } - - Future _callback() async { - await getMessages(); - BackgroundFetch.finish(); + BackgroundFetchConfig( + minimumFetchInterval: 15, + stopOnTerminate: false, + enableHeadless: true, + startOnBoot: true, + requiredNetworkType: NetworkType.ANY, + ), + (String taskId) async { + _logger.info("Callback (foreground) triggered, checking for new messages"); + await getMessages(); + _logger.info("Callback (foreground) finishing"); + BackgroundFetch.finish(taskId); + }, + ).then((value) { + _logger.info("Configured and started background fetch"); + _running = true; + }); } void start() async { diff --git a/lib/src/main/main_bloc.dart b/lib/src/main/main_bloc.dart index c1173758..09ab0ef3 100644 --- a/lib/src/main/main_bloc.dart +++ b/lib/src/main/main_bloc.dart @@ -174,7 +174,7 @@ class MainBloc extends Bloc { } Future _initCore() async { - await core.init(dbName); + await core.setupAsync(dbName); } Future _setupDefaultValues() async { diff --git a/lib/src/notifications/local_notification_manager.dart b/lib/src/notifications/local_notification_manager.dart index e2356399..da173508 100644 --- a/lib/src/notifications/local_notification_manager.dart +++ b/lib/src/notifications/local_notification_manager.dart @@ -71,6 +71,10 @@ class LocalNotificationManager { LocalNotificationManager._internal(); + LocalNotificationManager.recreate() { + _instance = LocalNotificationManager._internal(); + } + void setup() { _registerListeners(); } @@ -106,7 +110,7 @@ class LocalNotificationManager { await createInviteNotificationsAsync(); } - Future createChatNotificationsAsync() async { + Future createChatNotificationsAsync() async { final HashMap notificationHistory = await _getNotificationHistoryAsync(); final List freshMessages = await _context.getFreshMessages(); _temporaryMessageRepository.putIfAbsent(ids: freshMessages); diff --git a/pubspec.lock b/pubspec.lock index 8c0091aa..1ee3ba8f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -49,7 +49,7 @@ packages: name: background_fetch url: "https://pub.dartlang.org" source: hosted - version: "0.4.0" + version: "0.5.5" bloc: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 51f81c31..52e82bbe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ dependencies: flutter_localizations: sdk: flutter app_settings: ^3.0.0+1 - background_fetch: ^0.4.0 + background_fetch: ^0.5.5 bloc: ^3.0.0 cached_network_image: ^2.0.0 contacts_service: ^0.3.10