Skip to content

Commit

Permalink
Merge pull request #1380 from krille-chan/krille/better-notification-…
Browse files Browse the repository at this point in the history
…management

refactor: Reuse flutter local notifications object
  • Loading branch information
krille-chan authored Oct 3, 2024
2 parents 8b90c30 + 86c9354 commit 6e84830
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
56 changes: 36 additions & 20 deletions lib/utils/background_push.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,46 @@ class BackgroundPush {

bool upAction = false;

BackgroundPush._(this.client) {
firebase?.setListeners(
onMessage: (message) => pushHelper(
PushNotification.fromJson(
Map<String, dynamic>.from(message['data'] ?? message),
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(
Map<String, dynamic>.from(message['data'] ?? message),
),
client: client,
l10n: l10n,
activeRoomId: matrix?.activeRoomId,
flutterLocalNotificationsPlugin: _flutterLocalNotificationsPlugin,
),
client: client,
l10n: l10n,
activeRoomId: matrix?.activeRoomId,
onSelectNotification: goToRoom,
),
);
if (Platform.isAndroid) {
UnifiedPush.initialize(
onNewEndpoint: _newUpEndpoint,
onRegistrationFailed: _upUnregistered,
onUnregistered: _upUnregistered,
onMessage: _onUpMessage,
);
if (Platform.isAndroid) {
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(
Expand All @@ -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) {
Expand Down Expand Up @@ -400,6 +415,7 @@ class BackgroundPush {
client: client,
l10n: l10n,
activeRoomId: matrix?.activeRoomId,
flutterLocalNotificationsPlugin: _flutterLocalNotificationsPlugin,
);
}
}
Expand Down
28 changes: 3 additions & 25 deletions lib/utils/push_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,19 @@ Future<void> pushHelper(
Client? client,
L10n? l10n,
String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification,
required FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
}) async {
try {
await _tryPushHelper(
notification,
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,
Expand Down Expand Up @@ -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(
Expand All @@ -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(),
Expand Down

0 comments on commit 6e84830

Please sign in to comment.