Revert "refactor: Only initialize FlutterLocalNotificationsPlugin once"

This reverts commit d3a13705bd.
pull/1266/head
krille-chan 12 months ago
parent d3a13705bd
commit a928ecec1e
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.processNotification( onMessage: (message) => pushHelper(
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.processNotification( await pushHelper(
PushNotification.fromJson(data), PushNotification.fromJson(data),
client: client, client: client,
l10n: l10n, l10n: l10n,

@ -18,37 +18,13 @@ 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';
abstract class PushHelper { Future<void> 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,
@ -60,9 +36,15 @@ abstract class PushHelper {
} catch (e, s) { } catch (e, s) {
Logs().v('Push Helper has crashed!', e, s); Logs().v('Push Helper has crashed!', e, s);
final flutterLocalNotificationsPlugin = // Initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
await _getLocalNotificationsPlugin( final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
onSelectNotification: onSelectNotification, 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'));
@ -88,15 +70,15 @@ abstract class PushHelper {
); );
rethrow; rethrow;
} }
} }
static Future<void> _tryPushHelper( 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).',
@ -110,8 +92,15 @@ abstract class PushHelper {
return; return;
} }
final flutterLocalNotificationsPlugin = await _getLocalNotificationsPlugin( // Initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
onSelectNotification: onSelectNotification, 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(
@ -160,8 +149,7 @@ abstract class PushHelper {
client.backgroundSync = false; client.backgroundSync = false;
} }
if (event.type.startsWith('m.call') && if (event.type.startsWith('m.call') && event.type != EventTypes.CallInvite) {
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;
} }
@ -324,17 +312,17 @@ abstract class PushHelper {
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.
static Future<void> _setShortcut( 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(
@ -353,5 +341,4 @@ abstract class PushHelper {
isImportant: event.room.isFavourite, isImportant: event.room.isFavourite,
), ),
); );
}
} }

Loading…
Cancel
Save