feat: Desktop notifications on Linux Desktop

This commit is contained in:
Christian Pauly 2021-05-01 11:52:47 +02:00
parent 46f84b55de
commit 25e76f048e
3 changed files with 25 additions and 19 deletions

View File

@ -19,10 +19,7 @@ import 'package:provider/provider.dart';
import 'package:universal_html/html.dart' as html; import 'package:universal_html/html.dart' as html;
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
/*import 'package:fluffychat/views/chat_ui.dart'; import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:fluffychat/app_config.dart';
import 'package:dbus/dbus.dart';
import 'package:desktop_notifications/desktop_notifications.dart';*/
import '../../utils/beautify_string_extension.dart'; import '../../utils/beautify_string_extension.dart';
import '../../utils/localized_exception_extension.dart'; import '../../utils/localized_exception_extension.dart';
@ -184,6 +181,7 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
final room = client.getRoomById(roomId); final room = client.getRoomById(roomId);
if (room.notificationCount == 0) return; if (room.notificationCount == 0) return;
final event = Event.fromJson(eventUpdate.content, room); final event = Event.fromJson(eventUpdate.content, room);
final title = room.getLocalizedDisplayname(MatrixLocals(L10n.of(context)));
final body = event.getLocalizedBody( final body = event.getLocalizedBody(
MatrixLocals(L10n.of(context)), MatrixLocals(L10n.of(context)),
withSenderNamePrefix: withSenderNamePrefix:
@ -199,30 +197,22 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
..autoplay = true ..autoplay = true
..load(); ..load();
html.Notification( html.Notification(
room.getLocalizedDisplayname(MatrixLocals(L10n.of(context))), title,
body: body, body: body,
icon: icon.toString(), icon: icon.toString(),
); );
} else if (Platform.isLinux) { } else if (Platform.isLinux) {
/*var sessionBus = DBusClient.session(); await linuxNotifications.notify(
var client = NotificationClient(sessionBus); title,
_linuxNotificationIds[roomId] = await client.notify(
room.getLocalizedDisplayname(MatrixLocals(L10n.of(context))),
body: body, body: body,
replacesID: _linuxNotificationIds[roomId] ?? -1, replacesId: _linuxNotificationIds[roomId] ?? -1,
appName: AppConfig.applicationName, appName: AppConfig.applicationName,
actionCallback: (_) => Navigator.of(context, rootNavigator: false).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
ChatView(roomId),
),
(r) => r.isFirst),
); );
await sessionBus.close();*/
} }
} }
//final Map<String, int> _linuxNotificationIds = {}; final linuxNotifications = NotificationsClient();
final Map<String, int> _linuxNotificationIds = {};
@override @override
void initState() { void initState() {
@ -416,6 +406,8 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
onBlurSub?.cancel(); onBlurSub?.cancel();
_backgroundPush?.onLogin?.cancel(); _backgroundPush?.onLogin?.cancel();
linuxNotifications.close();
super.dispose(); super.dispose();
} }

View File

@ -183,6 +183,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.6.0" 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: disk_space_ns:
dependency: transitive dependency: transitive
description: description:

View File

@ -14,7 +14,7 @@ dependencies:
audioplayers: ^0.18.3 audioplayers: ^0.18.3
cached_network_image: ^3.0.0 cached_network_image: ^3.0.0
cupertino_icons: any 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 email_validator: ^2.0.1
emoji_picker_flutter: ^1.0.3 emoji_picker_flutter: ^1.0.3
famedlysdk: famedlysdk: