diff --git a/lib/main.dart b/lib/main.dart index 09ad6f068..e48b61a28 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -242,7 +242,7 @@ void main(List args) async { // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, // overlays: [SystemUiOverlay.bottom]); - await NotificationApi.init(); + unawaited(NotificationApi.init()); await loadCoinlibFuture; diff --git a/lib/services/notifications_api.dart b/lib/services/notifications_api.dart index a5a62c188..13dec90d7 100644 --- a/lib/services/notifications_api.dart +++ b/lib/services/notifications_api.dart @@ -8,13 +8,16 @@ * */ +import 'dart:async'; + import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import '../models/notification_model.dart'; import '../utilities/prefs.dart'; import 'notifications_service.dart'; -class NotificationApi { +abstract final class NotificationApi { + static Completer? _initCalledCompleter; static final _notifications = FlutterLocalNotificationsPlugin(); // static final onNotifications = BehaviorSubject(); @@ -33,6 +36,16 @@ class NotificationApi { } static Future init({bool initScheduled = false}) async { + if (_initCalledCompleter == null) { + _initCalledCompleter = Completer(); + } else { + if (_initCalledCompleter!.isCompleted) { + return; + } else { + return await _initCalledCompleter!.future; + } + } + const android = AndroidInitializationSettings('app_icon_alpha'); const iOS = DarwinInitializationSettings(); const linux = LinuxInitializationSettings( @@ -54,12 +67,18 @@ class NotificationApi { // onNotifications.add(payload.payload); // }, ); + _initCalledCompleter!.complete(); } - static Future clearNotifications() async => _notifications.cancelAll(); + static Future clearNotifications() async { + await init(); + await _notifications.cancelAll(); + } - static Future clearNotification(int id) async => - _notifications.cancel(id); + static Future clearNotification(int id) async { + await init(); + await _notifications.cancel(id); + } //=================================== static late Prefs prefs; @@ -79,6 +98,7 @@ class NotificationApi { String? changeNowId, String? payload, }) async { + await init(); await prefs.incrementCurrentNotificationIndex(); final id = prefs.currentNotificationId;