Merge pull request #1380 from krille-chan/krille/better-notification-management

refactor: Reuse flutter local notifications object
This commit is contained in:
Krille-chan 2024-10-03 17:06:25 +02:00 committed by GitHub
commit 6e8483004d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 45 deletions

View file

@ -70,31 +70,46 @@ class BackgroundPush {
bool upAction = false; bool upAction = false;
BackgroundPush._(this.client) { void _init() async {
firebase?.setListeners( try {
onMessage: (message) => pushHelper( await _flutterLocalNotificationsPlugin.initialize(
PushNotification.fromJson( const InitializationSettings(
Map<String, dynamic>.from(message['data'] ?? message), android: AndroidInitializationSettings('notifications_icon'),
iOS: DarwinInitializationSettings(),
), ),
client: client, onDidReceiveNotificationResponse: goToRoom,
l10n: l10n,
activeRoomId: matrix?.activeRoomId,
onSelectNotification: goToRoom,
),
);
if (Platform.isAndroid) {
UnifiedPush.initialize(
onNewEndpoint: _newUpEndpoint,
onRegistrationFailed: _upUnregistered,
onUnregistered: _upUnregistered,
onMessage: _onUpMessage,
); );
Logs().v('Flutter Local Notifications initialized');
firebase?.setListeners(
onMessage: (message) => pushHelper(
PushNotification.fromJson(
Map<String, dynamic>.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) { factory BackgroundPush.clientOnly(Client client) {
_instance ??= BackgroundPush._(client); return _instance ??= BackgroundPush._(client);
return _instance!;
} }
factory BackgroundPush( factory BackgroundPush(
@ -110,7 +125,7 @@ class BackgroundPush {
Future<void> cancelNotification(String roomId) async { Future<void> cancelNotification(String roomId) async {
Logs().v('Cancel notification for room', roomId); Logs().v('Cancel notification for room', roomId);
await FlutterLocalNotificationsPlugin().cancel(roomId.hashCode); await _flutterLocalNotificationsPlugin.cancel(roomId.hashCode);
// Workaround for app icon badge not updating // Workaround for app icon badge not updating
if (Platform.isIOS) { if (Platform.isIOS) {
@ -400,6 +415,7 @@ class BackgroundPush {
client: client, client: client,
l10n: l10n, l10n: l10n,
activeRoomId: matrix?.activeRoomId, activeRoomId: matrix?.activeRoomId,
flutterLocalNotificationsPlugin: _flutterLocalNotificationsPlugin,
); );
} }
} }

View file

@ -22,7 +22,7 @@ Future<void> pushHelper(
Client? client, Client? client,
L10n? l10n, L10n? l10n,
String? activeRoomId, String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification, required FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
}) async { }) async {
try { try {
await _tryPushHelper( await _tryPushHelper(
@ -30,22 +30,11 @@ Future<void> pushHelper(
client: client, client: client,
l10n: l10n, l10n: l10n,
activeRoomId: activeRoomId, activeRoomId: activeRoomId,
onSelectNotification: onSelectNotification, flutterLocalNotificationsPlugin: flutterLocalNotificationsPlugin,
); );
} catch (e, s) { } catch (e, s) {
Logs().v('Push Helper has crashed!', 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')); l10n ??= lookupL10n(const Locale('en'));
flutterLocalNotificationsPlugin.show( flutterLocalNotificationsPlugin.show(
notification.roomId?.hashCode ?? 0, notification.roomId?.hashCode ?? 0,
@ -76,7 +65,7 @@ Future<void> _tryPushHelper(
Client? client, Client? client,
L10n? l10n, L10n? l10n,
String? activeRoomId, String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification, required FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
}) async { }) async {
final isBackgroundMessage = client == null; final isBackgroundMessage = client == null;
Logs().v( Logs().v(
@ -91,17 +80,6 @@ Future<void> _tryPushHelper(
return; 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( client ??= (await ClientManager.getClients(
initialize: false, initialize: false,
store: await SharedPreferences.getInstance(), store: await SharedPreferences.getInstance(),