From 86c9354cfd2d2f071e5b630802871da07ce0a26b Mon Sep 17 00:00:00 2001 From: krille-chan Date: Thu, 3 Oct 2024 16:04:52 +0200 Subject: [PATCH] refactor: Reuse flutter local notifications object --- lib/utils/background_push.dart | 56 ++++++++++++++++++++++------------ lib/utils/push_helper.dart | 28 ++--------------- 2 files changed, 39 insertions(+), 45 deletions(-) diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart index 142ec7cf9..a7ce8c6de 100644 --- a/lib/utils/background_push.dart +++ b/lib/utils/background_push.dart @@ -70,31 +70,46 @@ class BackgroundPush { bool upAction = false; - BackgroundPush._(this.client) { - firebase?.setListeners( - onMessage: (message) => pushHelper( - PushNotification.fromJson( - Map.from(message['data'] ?? message), + void _init() async { + try { + await _flutterLocalNotificationsPlugin.initialize( + const InitializationSettings( + android: AndroidInitializationSettings('notifications_icon'), + iOS: DarwinInitializationSettings(), ), - client: client, - l10n: l10n, - activeRoomId: matrix?.activeRoomId, - onSelectNotification: goToRoom, - ), - ); - if (Platform.isAndroid) { - UnifiedPush.initialize( - onNewEndpoint: _newUpEndpoint, - onRegistrationFailed: _upUnregistered, - onUnregistered: _upUnregistered, - onMessage: _onUpMessage, + onDidReceiveNotificationResponse: goToRoom, ); + Logs().v('Flutter Local Notifications initialized'); + firebase?.setListeners( + onMessage: (message) => pushHelper( + PushNotification.fromJson( + Map.from(message['data'] ?? message), + ), + client: client, + l10n: l10n, + activeRoomId: matrix?.activeRoomId, + flutterLocalNotificationsPlugin: _flutterLocalNotificationsPlugin, + ), + ); + if (Platform.isAndroid) { + await UnifiedPush.initialize( + onNewEndpoint: _newUpEndpoint, + onRegistrationFailed: _upUnregistered, + onUnregistered: _upUnregistered, + onMessage: _onUpMessage, + ); + } + } catch (e, s) { + Logs().e('Unable to initialize Flutter local notifications', e, s); } } + BackgroundPush._(this.client) { + _init(); + } + factory BackgroundPush.clientOnly(Client client) { - _instance ??= BackgroundPush._(client); - return _instance!; + return _instance ??= BackgroundPush._(client); } factory BackgroundPush( @@ -110,7 +125,7 @@ class BackgroundPush { Future cancelNotification(String roomId) async { Logs().v('Cancel notification for room', roomId); - await FlutterLocalNotificationsPlugin().cancel(roomId.hashCode); + await _flutterLocalNotificationsPlugin.cancel(roomId.hashCode); // Workaround for app icon badge not updating if (Platform.isIOS) { @@ -400,6 +415,7 @@ class BackgroundPush { client: client, l10n: l10n, activeRoomId: matrix?.activeRoomId, + flutterLocalNotificationsPlugin: _flutterLocalNotificationsPlugin, ); } } diff --git a/lib/utils/push_helper.dart b/lib/utils/push_helper.dart index 66c2d0196..ff6c5934e 100644 --- a/lib/utils/push_helper.dart +++ b/lib/utils/push_helper.dart @@ -22,7 +22,7 @@ Future pushHelper( Client? client, L10n? l10n, String? activeRoomId, - void Function(NotificationResponse?)? onSelectNotification, + required FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin, }) async { try { await _tryPushHelper( @@ -30,22 +30,11 @@ Future pushHelper( client: client, l10n: l10n, activeRoomId: activeRoomId, - onSelectNotification: onSelectNotification, + flutterLocalNotificationsPlugin: flutterLocalNotificationsPlugin, ); } catch (e, s) { Logs().v('Push Helper has crashed!', e, s); - // Initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project - final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); - await flutterLocalNotificationsPlugin.initialize( - const InitializationSettings( - android: AndroidInitializationSettings('notifications_icon'), - iOS: DarwinInitializationSettings(), - ), - onDidReceiveNotificationResponse: onSelectNotification, - onDidReceiveBackgroundNotificationResponse: onSelectNotification, - ); - l10n ??= lookupL10n(const Locale('en')); flutterLocalNotificationsPlugin.show( notification.roomId?.hashCode ?? 0, @@ -76,7 +65,7 @@ Future _tryPushHelper( Client? client, L10n? l10n, String? activeRoomId, - void Function(NotificationResponse?)? onSelectNotification, + required FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin, }) async { final isBackgroundMessage = client == null; Logs().v( @@ -91,17 +80,6 @@ Future _tryPushHelper( return; } - // Initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project - final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); - await flutterLocalNotificationsPlugin.initialize( - const InitializationSettings( - android: AndroidInitializationSettings('notifications_icon'), - iOS: DarwinInitializationSettings(), - ), - onDidReceiveNotificationResponse: onSelectNotification, - //onDidReceiveBackgroundNotificationResponse: onSelectNotification, - ); - client ??= (await ClientManager.getClients( initialize: false, store: await SharedPreferences.getInstance(),