diff --git a/lib/main.dart b/lib/main.dart index 664464e7..aea790ad 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -70,7 +70,7 @@ class FlwtchState extends State { var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, newDesktopNotificationsManager(), globalAppState, globalServersList); cwtch = CwtchFfi(cwtchNotifier); } else { - var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager(), globalAppState, globalServersList); + var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, newDesktopNotificationsManager(), globalAppState, globalServersList); cwtch = CwtchFfi(cwtchNotifier); } print("initState: invoking cwtch.Start()"); diff --git a/lib/notification_manager.dart b/lib/notification_manager.dart index fa5de8c3..f01659be 100644 --- a/lib/notification_manager.dart +++ b/lib/notification_manager.dart @@ -1,6 +1,11 @@ +import 'dart:io'; + +import 'package:desktoasts/desktoasts.dart'; import 'package:desktop_notifications/desktop_notifications.dart'; import 'package:path/path.dart' as path; +import 'config.dart'; + // NotificationsManager provides a wrapper around platform specific notifications logic. abstract class NotificationsManager { Future notify(String message); @@ -26,15 +31,47 @@ class LinuxNotificationsManager implements NotificationsManager { } } +// Windows Notification Manager uses https://pub.dev/packages/desktoasts to implement +// windows notifications +class WindowsNotificationManager implements NotificationsManager { + late ToastService service; + + WindowsNotificationManager() { + service = new ToastService( + appName: 'Cwtch', + companyName: 'Open Privacy Research Society', + productName: 'Cwtch', + ); + } + + Future notify(String message) async { + Toast toast = new Toast( + type: ToastType.text01, + title: 'Cwtch', + subtitle: message, + ); + service.show(toast); + } +} + NotificationsManager newDesktopNotificationsManager() { - try { - // Test that we can actually access DBUS. Otherwise return a null - // notifications manager... - NotificationsClient client = NotificationsClient(); - client.getCapabilities(); - return LinuxNotificationsManager(client); - } catch (e) { - print("Attempted to access DBUS for notifications but failed. Switching off notifications."); + if (Platform.isLinux) { + try { + // Test that we can actually access DBUS. Otherwise return a null + // notifications manager... + NotificationsClient client = NotificationsClient(); + client.getCapabilities(); + return LinuxNotificationsManager(client); + } catch (e) { + EnvironmentConfig.debugLog( + "Attempted to access DBUS for notifications but failed. Switching off notifications."); + } + } else if (Platform.isWindows) { + try { + return WindowsNotificationManager(); + } catch (e) { + EnvironmentConfig.debugLog("Failed to create Windows desktoasts notification manager"); + } } return NullNotificationsManager(); } diff --git a/pubspec.lock b/pubspec.lock index 22a566d6..8f1137b3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -78,6 +78,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.5.2" + desktoasts: + dependency: "direct main" + description: + name: desktoasts + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.2" desktop_notifications: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 47e0b916..532fffce 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -44,6 +44,7 @@ dependencies: file_picker: ^4.3.2 file_picker_desktop: ^1.1.0 url_launcher: ^6.0.12 + desktoasts: ^0.0.2 dev_dependencies: msix: ^2.1.3 diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 4f788487..6859cc41 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -6,9 +6,12 @@ #include "generated_plugin_registrant.h" +#include #include void RegisterPlugins(flutter::PluginRegistry* registry) { + DesktoastsPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("DesktoastsPlugin")); UrlLauncherWindowsRegisterWithRegistrar( registry->GetRegistrarForPlugin("UrlLauncherWindows")); } diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index 411af46d..cb499b22 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + desktoasts url_launcher_windows )