mirror of
https://github.com/krille-chan/fluffychat
synced 2024-10-10 03:03:38 +00:00
Merge pull request #1380 from krille-chan/krille/better-notification-management
refactor: Reuse flutter local notifications object
This commit is contained in:
commit
6e8483004d
2 changed files with 39 additions and 45 deletions
|
@ -70,7 +70,16 @@ class BackgroundPush {
|
|||
|
||||
bool upAction = false;
|
||||
|
||||
BackgroundPush._(this.client) {
|
||||
void _init() async {
|
||||
try {
|
||||
await _flutterLocalNotificationsPlugin.initialize(
|
||||
const InitializationSettings(
|
||||
android: AndroidInitializationSettings('notifications_icon'),
|
||||
iOS: DarwinInitializationSettings(),
|
||||
),
|
||||
onDidReceiveNotificationResponse: goToRoom,
|
||||
);
|
||||
Logs().v('Flutter Local Notifications initialized');
|
||||
firebase?.setListeners(
|
||||
onMessage: (message) => pushHelper(
|
||||
PushNotification.fromJson(
|
||||
|
@ -79,22 +88,28 @@ class BackgroundPush {
|
|||
client: client,
|
||||
l10n: l10n,
|
||||
activeRoomId: matrix?.activeRoomId,
|
||||
onSelectNotification: goToRoom,
|
||||
flutterLocalNotificationsPlugin: _flutterLocalNotificationsPlugin,
|
||||
),
|
||||
);
|
||||
if (Platform.isAndroid) {
|
||||
UnifiedPush.initialize(
|
||||
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) {
|
||||
_instance ??= BackgroundPush._(client);
|
||||
return _instance!;
|
||||
return _instance ??= BackgroundPush._(client);
|
||||
}
|
||||
|
||||
factory BackgroundPush(
|
||||
|
@ -110,7 +125,7 @@ class BackgroundPush {
|
|||
|
||||
Future<void> cancelNotification(String roomId) async {
|
||||
Logs().v('Cancel notification for room', roomId);
|
||||
await FlutterLocalNotificationsPlugin().cancel(roomId.hashCode);
|
||||
await _flutterLocalNotificationsPlugin.cancel(roomId.hashCode);
|
||||
|
||||
// Workaround for app icon badge not updating
|
||||
if (Platform.isIOS) {
|
||||
|
@ -400,6 +415,7 @@ class BackgroundPush {
|
|||
client: client,
|
||||
l10n: l10n,
|
||||
activeRoomId: matrix?.activeRoomId,
|
||||
flutterLocalNotificationsPlugin: _flutterLocalNotificationsPlugin,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ Future<void> pushHelper(
|
|||
Client? client,
|
||||
L10n? l10n,
|
||||
String? activeRoomId,
|
||||
void Function(NotificationResponse?)? onSelectNotification,
|
||||
required FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
|
||||
}) async {
|
||||
try {
|
||||
await _tryPushHelper(
|
||||
|
@ -30,22 +30,11 @@ Future<void> pushHelper(
|
|||
client: client,
|
||||
l10n: l10n,
|
||||
activeRoomId: activeRoomId,
|
||||
onSelectNotification: onSelectNotification,
|
||||
flutterLocalNotificationsPlugin: flutterLocalNotificationsPlugin,
|
||||
);
|
||||
} 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,
|
||||
);
|
||||
|
||||
l10n ??= lookupL10n(const Locale('en'));
|
||||
flutterLocalNotificationsPlugin.show(
|
||||
notification.roomId?.hashCode ?? 0,
|
||||
|
@ -76,7 +65,7 @@ Future<void> _tryPushHelper(
|
|||
Client? client,
|
||||
L10n? l10n,
|
||||
String? activeRoomId,
|
||||
void Function(NotificationResponse?)? onSelectNotification,
|
||||
required FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
|
||||
}) async {
|
||||
final isBackgroundMessage = client == null;
|
||||
Logs().v(
|
||||
|
@ -91,17 +80,6 @@ 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,
|
||||
);
|
||||
|
||||
client ??= (await ClientManager.getClients(
|
||||
initialize: false,
|
||||
store: await SharedPreferences.getInstance(),
|
||||
|
|
Loading…
Reference in a new issue