|
@@ -73,6 +73,28 @@ impl Notification {
|
|
|
}
|
|
|
|
|
|
/// Shows the notification.
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ ///
|
|
|
+ /// ```no_run
|
|
|
+ /// use tauri::api::notification::Notification;
|
|
|
+ ///
|
|
|
+ /// // on an actual app, remove the string argument
|
|
|
+ /// let context = tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json");
|
|
|
+ /// Notification::new(&context.config().tauri.bundle.identifier)
|
|
|
+ /// .title("Tauri")
|
|
|
+ /// .body("Tauri is awesome!")
|
|
|
+ /// .show()
|
|
|
+ /// .unwrap();
|
|
|
+ /// ```
|
|
|
+ ///
|
|
|
+ /// ## Platform-specific
|
|
|
+ ///
|
|
|
+ /// - **Windows**: Not supported on Windows 7. If your app targets it, enable the `windows7-compat` feature and use [`Self::notify`].
|
|
|
+ #[cfg_attr(
|
|
|
+ all(not(doc_cfg), feature = "windows7-compat"),
|
|
|
+ deprecated = "This function does not work on Windows 7. Use `Self::notify` instead."
|
|
|
+ )]
|
|
|
pub fn show(self) -> crate::api::Result<()> {
|
|
|
let mut notification = notify_rust::Notification::new();
|
|
|
if let Some(body) = self.body {
|
|
@@ -108,7 +130,76 @@ impl Notification {
|
|
|
}
|
|
|
|
|
|
crate::async_runtime::spawn(async move {
|
|
|
- notification.show().expect("failed to show notification");
|
|
|
+ let _ = notification.show();
|
|
|
+ });
|
|
|
+
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Shows the notification. This API is similar to [`Self::show`], but it also works on Windows 7.
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ ///
|
|
|
+ /// ```no_run
|
|
|
+ /// use tauri::api::notification::Notification;
|
|
|
+ ///
|
|
|
+ /// // on an actual app, remove the string argument
|
|
|
+ /// let context = tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json");
|
|
|
+ /// let identifier = context.config().tauri.bundle.identifier.clone();
|
|
|
+ ///
|
|
|
+ /// tauri::Builder::default()
|
|
|
+ /// .setup(move |app| {
|
|
|
+ /// Notification::new(&identifier)
|
|
|
+ /// .title("Tauri")
|
|
|
+ /// .body("Tauri is awesome!")
|
|
|
+ /// .notify(&app.handle())
|
|
|
+ /// .unwrap();
|
|
|
+ /// Ok(())
|
|
|
+ /// })
|
|
|
+ /// .run(context)
|
|
|
+ /// .expect("error while running tauri application");
|
|
|
+ /// ```
|
|
|
+ #[cfg(feature = "windows7-compat")]
|
|
|
+ #[cfg_attr(doc_cfg, doc(cfg(feature = "windows7-compat")))]
|
|
|
+ #[allow(unused_variables)]
|
|
|
+ pub fn notify<R: crate::Runtime>(self, app: &crate::AppHandle<R>) -> crate::api::Result<()> {
|
|
|
+ #[cfg(windows)]
|
|
|
+ {
|
|
|
+ if crate::utils::platform::is_windows_7() {
|
|
|
+ self.notify_win7(app)
|
|
|
+ } else {
|
|
|
+ #[allow(deprecated)]
|
|
|
+ self.show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #[cfg(not(windows))]
|
|
|
+ {
|
|
|
+ #[allow(deprecated)]
|
|
|
+ self.show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #[cfg(all(windows, feature = "windows7-compat"))]
|
|
|
+ fn notify_win7<R: crate::Runtime>(self, app: &crate::AppHandle<R>) -> crate::api::Result<()> {
|
|
|
+ let app = app.clone();
|
|
|
+ let default_window_icon = app.manager.inner.default_window_icon.clone();
|
|
|
+ let _ = app.run_on_main_thread(move || {
|
|
|
+ let mut notification = win7_notifications::Notification::new();
|
|
|
+ if let Some(body) = self.body {
|
|
|
+ notification.body(&body);
|
|
|
+ }
|
|
|
+ if let Some(title) = self.title {
|
|
|
+ notification.summary(&title);
|
|
|
+ }
|
|
|
+ if let Some(crate::Icon::Rgba {
|
|
|
+ rgba,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ }) = default_window_icon
|
|
|
+ {
|
|
|
+ notification.icon(rgba, width, height);
|
|
|
+ }
|
|
|
+ let _ = notification.show();
|
|
|
});
|
|
|
|
|
|
Ok(())
|