@@ -40,6 +40,50 @@ pub struct Notification {
4040 icon : Option < String > ,
4141 /// The notification identifier
4242 identifier : String ,
43+ /// The notification sound
44+ sound : Option < Sound > ,
45+ }
46+
47+ /// Notification sound.
48+ #[ derive( Debug ) ]
49+ pub enum Sound {
50+ /// The default notification sound.
51+ Default ,
52+ /// A custom notification sound.
53+ ///
54+ /// ## Platform-specific
55+ ///
56+ /// Each OS has a different sound name so you will need to conditionally specify an appropriate sound
57+ /// based on the OS in use, for a list of sounds see:
58+ /// - **Linux**: can be one of the sounds listed in <https://0pointer.de/public/sound-naming-spec.html>
59+ /// - **Windows**: can be one of the sounds listed in <https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio>
60+ /// but without the prefix, for example, if `ms-winsoundevent:Notification.Default` you would use `Default` and
61+ /// if `ms-winsoundevent:Notification.Looping.Alarm2`, you would use `Alarm2`.
62+ /// Windows 7 is not supported, if a sound is provided, it will play the default sound, otherwise it will be silent.
63+ /// - **macOS**: you can specify the name of the sound you'd like to play when the notification is shown.
64+ /// Any of the default sounds (under System Preferences > Sound) can be used, in addition to custom sound files.
65+ /// Be sure that the sound file is under one of the following locations:
66+ /// - `~/Library/Sounds`
67+ /// - `/Library/Sounds`
68+ /// - `/Network/Library/Sounds`
69+ /// - `/System/Library/Sounds`
70+ ///
71+ /// See the [`NSSound`] docs for more information.
72+ ///
73+ /// [`NSSound`]: https://developer.apple.com/documentation/appkit/nssound
74+ Custom ( String ) ,
75+ }
76+
77+ impl From < String > for Sound {
78+ fn from ( value : String ) -> Self {
79+ Self :: Custom ( value)
80+ }
81+ }
82+
83+ impl From < & str > for Sound {
84+ fn from ( value : & str ) -> Self {
85+ Self :: Custom ( value. into ( ) )
86+ }
4387}
4488
4589impl Notification {
@@ -72,6 +116,15 @@ impl Notification {
72116 self
73117 }
74118
119+ /// Sets the notification sound. By default the notification has no sound.
120+ ///
121+ /// See [`Sound`] for more information.
122+ #[ must_use]
123+ pub fn sound ( mut self , sound : impl Into < Sound > ) -> Self {
124+ self . sound . replace ( sound. into ( ) ) ;
125+ self
126+ }
127+
75128 /// Shows the notification.
76129 ///
77130 /// # Examples
@@ -108,6 +161,17 @@ impl Notification {
108161 } else {
109162 notification. auto_icon ( ) ;
110163 }
164+ if let Some ( sound) = self . sound {
165+ notification. sound_name ( & match sound {
166+ #[ cfg( target_os = "macos" ) ]
167+ Sound :: Default => "NSUserNotificationDefaultSoundName" . to_string ( ) ,
168+ #[ cfg( windows) ]
169+ Sound :: Default => "Default" . to_string ( ) ,
170+ #[ cfg( all( unix, not( target_os = "macos" ) ) ) ]
171+ Sound :: Default => "message-new-instant" . to_string ( ) ,
172+ Sound :: Custom ( c) => c,
173+ } ) ;
174+ }
111175 #[ cfg( windows) ]
112176 {
113177 let exe = tauri_utils:: platform:: current_exe ( ) ?;
@@ -191,6 +255,7 @@ impl Notification {
191255 if let Some ( title) = self . title {
192256 notification. summary ( & title) ;
193257 }
258+ notification. silent ( self . sound . is_none ( ) ) ;
194259 if let Some ( crate :: Icon :: Rgba {
195260 rgba,
196261 width,
0 commit comments