refactor: Reuse flutter local notifications object

This commit is contained in:
krille-chan 2024-10-03 16:04:52 +02:00
commit 86c9354cfd
No known key found for this signature in database
2 changed files with 39 additions and 45 deletions

View file

@ -22,7 +22,7 @@ Future<void> pushHelper(
Client? client,
L10n? l10n,
String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification,
required FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
}) async {
try {
await _tryPushHelper(
@ -30,22 +30,11 @@ Future<void> 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<void> _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<void> _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(),