From 25e76f048e6a560d31177e1ef2b0223c2ed6b8d8 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 1 May 2021 11:52:47 +0200 Subject: [PATCH] feat: Desktop notifications on Linux Desktop --- lib/views/widgets/matrix.dart | 28 ++++++++++------------------ pubspec.lock | 14 ++++++++++++++ pubspec.yaml | 2 +- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/views/widgets/matrix.dart b/lib/views/widgets/matrix.dart index 2426a84c..fe764523 100644 --- a/lib/views/widgets/matrix.dart +++ b/lib/views/widgets/matrix.dart @@ -19,10 +19,7 @@ import 'package:provider/provider.dart'; import 'package:universal_html/html.dart' as html; import 'package:http/http.dart' as http; import 'package:url_launcher/url_launcher.dart'; -/*import 'package:fluffychat/views/chat_ui.dart'; -import 'package:fluffychat/app_config.dart'; -import 'package:dbus/dbus.dart'; -import 'package:desktop_notifications/desktop_notifications.dart';*/ +import 'package:desktop_notifications/desktop_notifications.dart'; import '../../utils/beautify_string_extension.dart'; import '../../utils/localized_exception_extension.dart'; @@ -184,6 +181,7 @@ class MatrixState extends State with WidgetsBindingObserver { final room = client.getRoomById(roomId); if (room.notificationCount == 0) return; final event = Event.fromJson(eventUpdate.content, room); + final title = room.getLocalizedDisplayname(MatrixLocals(L10n.of(context))); final body = event.getLocalizedBody( MatrixLocals(L10n.of(context)), withSenderNamePrefix: @@ -199,30 +197,22 @@ class MatrixState extends State with WidgetsBindingObserver { ..autoplay = true ..load(); html.Notification( - room.getLocalizedDisplayname(MatrixLocals(L10n.of(context))), + title, body: body, icon: icon.toString(), ); } else if (Platform.isLinux) { - /*var sessionBus = DBusClient.session(); - var client = NotificationClient(sessionBus); - _linuxNotificationIds[roomId] = await client.notify( - room.getLocalizedDisplayname(MatrixLocals(L10n.of(context))), + await linuxNotifications.notify( + title, body: body, - replacesID: _linuxNotificationIds[roomId] ?? -1, + replacesId: _linuxNotificationIds[roomId] ?? -1, appName: AppConfig.applicationName, - actionCallback: (_) => Navigator.of(context, rootNavigator: false).pushAndRemoveUntil( - AppRoute.defaultRoute( - context, - ChatView(roomId), - ), - (r) => r.isFirst), ); - await sessionBus.close();*/ } } - //final Map _linuxNotificationIds = {}; + final linuxNotifications = NotificationsClient(); + final Map _linuxNotificationIds = {}; @override void initState() { @@ -416,6 +406,8 @@ class MatrixState extends State with WidgetsBindingObserver { onBlurSub?.cancel(); _backgroundPush?.onLogin?.cancel(); + linuxNotifications.close(); + super.dispose(); } diff --git a/pubspec.lock b/pubspec.lock index 9ad2b195..d37b23b5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -183,6 +183,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.6.0" + dbus: + dependency: transitive + description: + name: dbus + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.3" + desktop_notifications: + dependency: "direct main" + description: + name: desktop_notifications + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.0" disk_space_ns: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 91597344..8b9b6cc7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,7 +14,7 @@ dependencies: audioplayers: ^0.18.3 cached_network_image: ^3.0.0 cupertino_icons: any - # desktop_notifications: ^0.0.0-dev.4 // Currently blocked by: https://github.com/canonical/desktop_notifications.dart/issues/5 + desktop_notifications: ^0.4.0 email_validator: ^2.0.1 emoji_picker_flutter: ^1.0.3 famedlysdk: