refactor: Only initialize FlutterLocalNotificationsPlugin once

pull/1266/head
krille-chan 7 months ago
parent e5bbb755d9
commit d3a13705bd
No known key found for this signature in database

@ -71,7 +71,7 @@ class BackgroundPush {
BackgroundPush._(this.client) { BackgroundPush._(this.client) {
firebase?.setListeners( firebase?.setListeners(
onMessage: (message) => pushHelper( onMessage: (message) => PushHelper.processNotification(
PushNotification.fromJson( PushNotification.fromJson(
Map<String, dynamic>.from(message['data'] ?? message), Map<String, dynamic>.from(message['data'] ?? message),
), ),
@ -393,7 +393,7 @@ class BackgroundPush {
); );
// UP may strip the devices list // UP may strip the devices list
data['devices'] ??= []; data['devices'] ??= [];
await pushHelper( await PushHelper.processNotification(
PushNotification.fromJson(data), PushNotification.fromJson(data),
client: client, client: client,
l10n: l10n, l10n: l10n,

@ -18,13 +18,37 @@ import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/voip/callkeep_manager.dart'; import 'package:fluffychat/utils/voip/callkeep_manager.dart';
Future<void> pushHelper( abstract class PushHelper {
static FlutterLocalNotificationsPlugin? _flutterLocalNotificationsPlugin;
static Future<FlutterLocalNotificationsPlugin> _getLocalNotificationsPlugin({
void Function(NotificationResponse?)? onSelectNotification,
}) async {
var flutterlocalNotifcationsPlugin = _flutterLocalNotificationsPlugin;
if (flutterlocalNotifcationsPlugin != null) {
return flutterlocalNotifcationsPlugin;
}
flutterlocalNotifcationsPlugin =
_flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterlocalNotifcationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings('notifications_icon'),
iOS: DarwinInitializationSettings(),
),
onDidReceiveNotificationResponse: onSelectNotification,
onDidReceiveBackgroundNotificationResponse: onSelectNotification,
);
return flutterlocalNotifcationsPlugin;
}
static Future<void> processNotification(
PushNotification notification, { PushNotification notification, {
Client? client, Client? client,
L10n? l10n, L10n? l10n,
String? activeRoomId, String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification, void Function(NotificationResponse?)? onSelectNotification,
}) async { }) async {
try { try {
await _tryPushHelper( await _tryPushHelper(
notification, notification,
@ -36,15 +60,9 @@ Future<void> pushHelper(
} 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 =
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); await _getLocalNotificationsPlugin(
await flutterLocalNotificationsPlugin.initialize( onSelectNotification: onSelectNotification,
const InitializationSettings(
android: AndroidInitializationSettings('notifications_icon'),
iOS: DarwinInitializationSettings(),
),
onDidReceiveNotificationResponse: onSelectNotification,
onDidReceiveBackgroundNotificationResponse: onSelectNotification,
); );
l10n ??= lookupL10n(const Locale('en')); l10n ??= lookupL10n(const Locale('en'));
@ -70,15 +88,15 @@ Future<void> pushHelper(
); );
rethrow; rethrow;
} }
} }
Future<void> _tryPushHelper( static Future<void> _tryPushHelper(
PushNotification notification, { PushNotification notification, {
Client? client, Client? client,
L10n? l10n, L10n? l10n,
String? activeRoomId, String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification, void Function(NotificationResponse?)? onSelectNotification,
}) async { }) async {
final isBackgroundMessage = client == null; final isBackgroundMessage = client == null;
Logs().v( Logs().v(
'Push helper has been started (background=$isBackgroundMessage).', 'Push helper has been started (background=$isBackgroundMessage).',
@ -92,15 +110,8 @@ 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 = await _getLocalNotificationsPlugin(
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); onSelectNotification: onSelectNotification,
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings('notifications_icon'),
iOS: DarwinInitializationSettings(),
),
onDidReceiveNotificationResponse: onSelectNotification,
//onDidReceiveBackgroundNotificationResponse: onSelectNotification,
); );
client ??= (await ClientManager.getClients( client ??= (await ClientManager.getClients(
@ -149,7 +160,8 @@ Future<void> _tryPushHelper(
client.backgroundSync = false; client.backgroundSync = false;
} }
if (event.type.startsWith('m.call') && event.type != EventTypes.CallInvite) { if (event.type.startsWith('m.call') &&
event.type != EventTypes.CallInvite) {
Logs().v('Push message is a m.call but not invite. Do not display.'); Logs().v('Push message is a m.call but not invite. Do not display.');
return; return;
} }
@ -312,17 +324,17 @@ Future<void> _tryPushHelper(
payload: event.roomId, payload: event.roomId,
); );
Logs().v('Push helper has been completed!'); Logs().v('Push helper has been completed!');
} }
/// Creates a shortcut for Android platform but does not block displaying the /// Creates a shortcut for Android platform but does not block displaying the
/// notification. This is optional but provides a nicer view of the /// notification. This is optional but provides a nicer view of the
/// notification popup. /// notification popup.
Future<void> _setShortcut( static Future<void> _setShortcut(
Event event, Event event,
L10n l10n, L10n l10n,
String title, String title,
File? avatarFile, File? avatarFile,
) async { ) async {
final flutterShortcuts = FlutterShortcuts(); final flutterShortcuts = FlutterShortcuts();
await flutterShortcuts.initialize(debug: !kReleaseMode); await flutterShortcuts.initialize(debug: !kReleaseMode);
await flutterShortcuts.pushShortcutItem( await flutterShortcuts.pushShortcutItem(
@ -341,4 +353,5 @@ Future<void> _setShortcut(
isImportant: event.room.isFavourite, isImportant: event.room.isFavourite,
), ),
); );
}
} }

Loading…
Cancel
Save