refactor: Only initialize FlutterLocalNotificationsPlugin once
This commit is contained in:
parent
e5bbb755d9
commit
d3a13705bd
2 changed files with 309 additions and 296 deletions
|
|
@ -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,7 +18,31 @@ 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,
|
||||||
|
|
@ -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'));
|
||||||
|
|
@ -72,7 +90,7 @@ Future<void> pushHelper(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _tryPushHelper(
|
static Future<void> _tryPushHelper(
|
||||||
PushNotification notification, {
|
PushNotification notification, {
|
||||||
Client? client,
|
Client? client,
|
||||||
L10n? l10n,
|
L10n? l10n,
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
@ -317,7 +329,7 @@ Future<void> _tryPushHelper(
|
||||||
/// 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,
|
||||||
|
|
@ -342,3 +354,4 @@ Future<void> _setShortcut(
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue