Skip to content

Commit 2e18ccc

Browse files
authored
Merge pull request #3 from DemonExposer/linux-improvement
Linux improvement
2 parents 01684a3 + fd61c65 commit 2e18ccc

3 files changed

Lines changed: 44 additions & 16 deletions

File tree

OsNotifications/Notifications.cs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
1-
using System.ComponentModel;
2-
using System.Diagnostics;
3-
using System.Reflection;
1+
using System.Reflection;
42
using System.Runtime.InteropServices;
3+
using Tmds.DBus;
54

65
namespace 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+
820
public 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
}

OsNotifications/OsNotifications.csproj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net8.0</TargetFramework>
3+
<TargetFramework>net10.0</TargetFramework>
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7-
<Version>1.1.3</Version>
7+
<Version>1.1.4</Version>
88
<Authors>DemonExposer</Authors>
99
<Description>Native OS notifications in .NET</Description>
1010
<RepositoryUrl>https://github.com/DemonExposer/OsNotifications</RepositoryUrl>
1111
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1212
<PackageProjectUrl>https://github.com/DemonExposer/OsNotifications</PackageProjectUrl>
1313
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
15+
<PackageTags>notifications;os-notifications;console;GUI;macos;linux;windows;user-notifications;toast-notifications;popup-notifications;native-notifications</PackageTags>
1516
<Title>OsNotifications</Title>
16-
<Copyright>Copyright (c) 2025 DemonExposer</Copyright>
17+
<Copyright>Copyright (c) 2026 DemonExposer</Copyright>
1718
</PropertyGroup>
1819

1920
<Target Name="CompileMacDependencies" BeforeTargets="Build">
@@ -66,6 +67,10 @@
6667
</Content>
6768
</ItemGroup>
6869

70+
<ItemGroup>
71+
<PackageReference Include="Tmds.DBus" Version="0.91.1" />
72+
</ItemGroup>
73+
6974
<Target Name="CleanLib" AfterTargets="Clean">
7075
<RemoveDir Directories="$(OutputPath)lib" Condition="Exists('$(OutputPath)lib')"/>
7176
</Target>

WindowsNotification/WindowsNotification.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
4+
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<EnableWindowsTargeting>true</EnableWindowsTargeting>

0 commit comments

Comments
 (0)