chore: Make push helper more fail safe

This commit is contained in:
Christian Pauly 2022-07-30 12:49:51 +02:00
parent 06d3a3a4c8
commit 8c9bda338b

View File

@ -19,6 +19,43 @@ Future<void> pushHelper(
L10n? l10n,
String? activeRoomId,
Future<dynamic> Function(String?)? onSelectNotification,
}) async {
try {
await _tryPushHelper(
notification,
client: client,
l10n: l10n,
activeRoomId: activeRoomId,
onSelectNotification: onSelectNotification,
);
} catch (e, s) {
Logs().wtf('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: IOSInitializationSettings(),
),
onSelectNotification: onSelectNotification,
);
_flutterLocalNotificationsPlugin.show(
0,
l10n?.newMessageInFluffyChat,
l10n?.openAppToReadMessages,
null,
);
rethrow;
}
}
Future<void> _tryPushHelper(
PushNotification notification, {
Client? client,
L10n? l10n,
String? activeRoomId,
Future<dynamic> Function(String?)? onSelectNotification,
}) async {
final isBackgroundMessage = client == null;
Logs().v(
@ -52,11 +89,13 @@ Future<void> pushHelper(
if (event == null) {
Logs().v('Notification is a clearing indicator.');
if (notification.counts == null || notification.counts?.unread == 0) {
await _flutterLocalNotificationsPlugin.cancelAll();
final store = await SharedPreferences.getInstance();
await store.setString(
SettingKeys.notificationCurrentIds, json.encode({}));
if (notification.counts?.unread == 0) {
if (notification.counts == null || notification.counts?.unread == 0) {
await _flutterLocalNotificationsPlugin.cancelAll();
final store = await SharedPreferences.getInstance();
await store.setString(
SettingKeys.notificationCurrentIds, json.encode({}));
}
}
return;
}