Skip to content

Commit

Permalink
refactor: Reuse flutter local notifications object
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Oct 3, 2024
1 parent 8b90c30 commit 1d7ef84
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
53 changes: 35 additions & 18 deletions lib/utils/background_push.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,45 @@ 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,
onDidReceiveBackgroundNotificationResponse: 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) {
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!;
Expand All @@ -110,7 +127,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
29 changes: 4 additions & 25 deletions lib/utils/push_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,20 @@ Future<void> pushHelper(
Client? client,
L10n? l10n,
String? activeRoomId,
void Function(NotificationResponse?)? onSelectNotification,
FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin,
}) async {
flutterLocalNotificationsPlugin ??= FlutterLocalNotificationsPlugin();
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 +66,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 +81,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 1d7ef84

Please sign in to comment.