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) {
firebase?.setListeners(
onMessage: (message) => pushHelper(
onMessage: (message) => PushHelper.processNotification(
PushNotification.fromJson(
Map<String, dynamic>.from(message['data'] ?? message),
),
@ -393,7 +393,7 @@ class BackgroundPush {
);
// UP may strip the devices list
data['devices'] ??= [];
await pushHelper(
await PushHelper.processNotification(
PushNotification.fromJson(data),
client: client,
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/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, {
Client? client,
L10n? l10n,
String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification,
}) async {
}) async {
try {
await _tryPushHelper(
notification,
@ -36,15 +60,9 @@ Future<void> pushHelper(
} 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,
final flutterLocalNotificationsPlugin =
await _getLocalNotificationsPlugin(
onSelectNotification: onSelectNotification,
);
l10n ??= lookupL10n(const Locale('en'));
@ -70,15 +88,15 @@ Future<void> pushHelper(
);
rethrow;
}
}
}
Future<void> _tryPushHelper(
static Future<void> _tryPushHelper(
PushNotification notification, {
Client? client,
L10n? l10n,
String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification,
}) async {
}) async {
final isBackgroundMessage = client == null;
Logs().v(
'Push helper has been started (background=$isBackgroundMessage).',
@ -92,15 +110,8 @@ 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,
final flutterLocalNotificationsPlugin = await _getLocalNotificationsPlugin(
onSelectNotification: onSelectNotification,
);
client ??= (await ClientManager.getClients(
@ -149,7 +160,8 @@ Future<void> _tryPushHelper(
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.');
return;
}
@ -312,17 +324,17 @@ Future<void> _tryPushHelper(
payload: event.roomId,
);
Logs().v('Push helper has been completed!');
}
}
/// Creates a shortcut for Android platform but does not block displaying the
/// notification. This is optional but provides a nicer view of the
/// notification popup.
Future<void> _setShortcut(
/// Creates a shortcut for Android platform but does not block displaying the
/// notification. This is optional but provides a nicer view of the
/// notification popup.
static Future<void> _setShortcut(
Event event,
L10n l10n,
String title,
File? avatarFile,
) async {
) async {
final flutterShortcuts = FlutterShortcuts();
await flutterShortcuts.initialize(debug: !kReleaseMode);
await flutterShortcuts.pushShortcutItem(
@ -341,4 +353,5 @@ Future<void> _setShortcut(
isImportant: event.room.isFavourite,
),
);
}
}

Loading…
Cancel
Save