1- using System . ComponentModel ;
2- using System . Diagnostics ;
3- using System . Reflection ;
1+ using System . Reflection ;
42using System . Runtime . InteropServices ;
3+ using Tmds . DBus ;
54
65namespace OsNotifications ;
76
7+ [ DBusInterface ( "org.freedesktop.Notifications" ) ]
8+ public interface INotifier : IDBusObject {
9+ Task < uint > NotifyAsync (
10+ string appName ,
11+ uint replacesId ,
12+ string appIcon ,
13+ string summary ,
14+ string body ,
15+ string [ ] actions ,
16+ IDictionary < string , object > hints ,
17+ int expireTimeout ) ;
18+ }
19+
820public partial class Notifications {
921 public static string BundleIdentifier = "" ;
22+
1023 public static Uri ? WindowsAudioSource {
11- get => _windowsAudioSource ;
24+ get ;
1225 set {
13- _windowsAudioSource = value ;
26+ field = value ;
1427 _playDefaultWindowsSound = false ;
1528 }
1629 }
1730
18- private static Uri ? _windowsAudioSource = null ;
1931 private static bool _isApplicationTypeSpecified ;
2032 private static bool _playDefaultWindowsSound = true ;
33+ private static readonly INotifier ? Notifier ;
34+
35+ static Notifications ( ) {
36+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
37+ return ;
38+
39+ Connection ? connection = Connection . Session ;
40+ connection . ConnectAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
41+ Notifier = connection . CreateProxy < INotifier > (
42+ "org.freedesktop.Notifications" ,
43+ "/org/freedesktop/Notifications" ) ;
44+
45+ AppDomain . CurrentDomain . ProcessExit += ( _ , _ ) => connection . Dispose ( ) ;
46+ }
2147
2248 public static void ResetWindowsAudioSource ( ) => _playDefaultWindowsSound = true ;
2349
@@ -47,11 +73,8 @@ public static void ShowNotification(string title, string message = "", string in
4773 }
4874
4975 private static void ShowNotificationLinux ( string title , string message ) {
50- try {
51- Process . Start ( "notify-send" , $ "\" { title } \" \" { message } \" ") . WaitForExit ( ) ;
52- } catch ( Win32Exception ) {
53- throw new PlatformNotSupportedException ( "Notifications are not supported on this Linux distro" ) ;
54- }
76+ Notifier ! . NotifyAsync ( Assembly . GetEntryAssembly ( ) ? . GetName ( ) . Name ?? "" , 0 , "" , title , message ,
77+ [ ] , new Dictionary < string , object > ( ) , 5000 ) . GetAwaiter ( ) . GetResult ( ) ;
5578 }
5679
5780 private static void ShowNotificationMac ( string title , string message , string informativeText ) {
@@ -77,6 +100,6 @@ private static void ShowNotificationWindows(string title, string message) {
77100 MethodInfo ? showNotificationMethod = windowsNotificationClass ? . GetMethod ( "ShowNotification" ) ;
78101
79102 object ? instance = Activator . CreateInstance ( windowsNotificationClass ! ) ;
80- showNotificationMethod ? . Invoke ( instance , [ title , message , ! _playDefaultWindowsSound , _windowsAudioSource ] ) ;
103+ showNotificationMethod ? . Invoke ( instance , [ title , message , ! _playDefaultWindowsSound , WindowsAudioSource ] ) ;
81104 }
82105}
0 commit comments