From 3829c7f879377e39d9269a2b773bc8a29a51b03d Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Mon, 11 Nov 2024 16:50:33 -0800 Subject: [PATCH 1/9] feat(push): push notifications support - feat(push): relay events for push notifications registration and errors to app - feat(push): gate apns by `push-notifications` feature Relates-To: tauri-apps/tauri#11651 Signed-off-by: Sam Gammon --- crates/tauri-runtime-wry/Cargo.toml | 1 + crates/tauri-runtime-wry/src/lib.rs | 14 ++++++++++++++ crates/tauri-runtime/Cargo.toml | 1 + crates/tauri-runtime/src/lib.rs | 7 +++++++ crates/tauri/Cargo.toml | 1 + crates/tauri/src/app.rs | 14 ++++++++++++++ crates/tauri/src/lib.rs | 3 +++ 7 files changed, 41 insertions(+) diff --git a/crates/tauri-runtime-wry/Cargo.toml b/crates/tauri-runtime-wry/Cargo.toml index 125b1de598cd..8e3efe4121ca 100644 --- a/crates/tauri-runtime-wry/Cargo.toml +++ b/crates/tauri-runtime-wry/Cargo.toml @@ -64,6 +64,7 @@ jni = "0.21" [features] devtools = ["wry/devtools", "tauri-runtime/devtools"] +push-notifications = ["tao/push-notifications"] macos-private-api = [ "wry/fullscreen", "wry/transparent", diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 12629db18420..0d662a7cec8c 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -1351,6 +1351,10 @@ pub enum Message { Box (String, TaoWindowBuilder) + Send>, Sender>>, ), + #[cfg(feature = "push-notifications")] + PushRegistration(PushToken), + #[cfg(feature = "push-notifications")] + PushRegistrationFailed(String), UserEvent(T), } @@ -3551,6 +3555,11 @@ fn handle_user_message( } } + #[cfg(feature = "push-notifications")] + Message::PushRegistration(_) => (), + #[cfg(feature = "push-notifications")] + Message::PushRegistrationFailed(_) => (), + Message::UserEvent(_) => (), Message::EventLoopWindowTarget(message) => match message { EventLoopWindowTargetMessage::CursorPosition(sender) => { @@ -3805,6 +3814,11 @@ fn handle_event_loop( } => callback(RunEvent::Reopen { has_visible_windows, }), + #[cfg(feature = "push-notifications")] + Event::PushRegistration(token) => callback(RunEvent::PushRegistration(token)), + #[cfg(feature = "push-notifications")] + Event::PushRegistrationError(token) => callback(RunEvent::PushRegistrationFailed(token)), + _ => (), } } diff --git a/crates/tauri-runtime/Cargo.toml b/crates/tauri-runtime/Cargo.toml index 6a2110af08a0..14b19f01c079 100644 --- a/crates/tauri-runtime/Cargo.toml +++ b/crates/tauri-runtime/Cargo.toml @@ -51,3 +51,4 @@ url = "2" [features] devtools = [] macos-private-api = [] +push-notifications = [] diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index 1257dab15f86..d4e0d0771d2a 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -47,6 +47,7 @@ pub use dpi; pub type WindowEventId = u32; pub type WebviewEventId = u32; +pub type PushToken = Vec; /// A rectangular region. #[derive(Clone, Copy, Debug, Serialize)] @@ -248,6 +249,12 @@ pub enum RunEvent { /// Indicates whether the NSApplication object found any visible windows in your application. has_visible_windows: bool, }, + /// Push token was registered. + #[cfg(feature = "push-notifications")] + PushRegistration(PushToken), + /// Push token failure. + #[cfg(feature = "push-notifications")] + PushRegistrationFailed(String), /// A custom event defined by the user. UserEvent(T), } diff --git a/crates/tauri/Cargo.toml b/crates/tauri/Cargo.toml index 5acb944033e0..755be7341cf7 100644 --- a/crates/tauri/Cargo.toml +++ b/crates/tauri/Cargo.toml @@ -169,6 +169,7 @@ tray-icon = ["dep:tray-icon"] tracing = ["dep:tracing", "tauri-macros/tracing", "tauri-runtime-wry/tracing"] test = [] compression = ["tauri-macros/compression", "tauri-utils/compression"] +push-notifications = ["tauri-runtime/push-notifications", "tauri-runtime-wry/push-notifications"] wry = ["tauri-runtime-wry"] objc-exception = ["tauri-runtime-wry/objc-exception"] linux-libxdo = ["tray-icon/libxdo", "muda/libxdo"] diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 509ba340c8f4..eeec7ff89e7a 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -69,6 +69,10 @@ pub type OnPageLoad = dyn Fn(&Webview, &PageLoadPayload<'_>) + Send + Sync pub type ChannelInterceptor = Box, CallbackFn, usize, &InvokeResponseBody) -> bool + Send + Sync + 'static>; +/// Push notifications token type. +#[cfg(feature = "push-notifications")] +pub type PushToken = Vec; + /// The exit code on [`RunEvent::ExitRequested`] when [`AppHandle#method.restart`] is called. pub const RESTART_EXIT_CODE: i32 = i32::MAX; @@ -246,6 +250,12 @@ pub enum RunEvent { /// Indicates whether the NSApplication object found any visible windows in your application. has_visible_windows: bool, }, + #[cfg(feature = "push-notifications")] + /// Indicates that a push token has become available. + PushRegistration(PushToken), + #[cfg(feature = "push-notifications")] + /// Indicates that an error occurred while registering for push notification services. + PushRegistrationFailed(String), } impl From for RunEvent { @@ -2201,6 +2211,10 @@ fn on_event_loop_event( } RuntimeRunEvent::Resumed => RunEvent::Resumed, RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared, + #[cfg(feature = "push-notifications")] + RuntimeRunEvent::PushRegistration(t) => RunEvent::PushRegistration(t), + #[cfg(feature = "push-notifications")] + RuntimeRunEvent::PushRegistrationFailed(err) => RunEvent::PushRegistrationFailed(err), RuntimeRunEvent::UserEvent(t) => { match t { #[cfg(desktop)] diff --git a/crates/tauri/src/lib.rs b/crates/tauri/src/lib.rs index e37df1624d07..4101b053e90b 100644 --- a/crates/tauri/src/lib.rs +++ b/crates/tauri/src/lib.rs @@ -79,6 +79,9 @@ pub use tauri_macros::{command, generate_handler}; use tauri_utils::assets::AssetsIter; pub use url::Url; +#[cfg(feature = "push-notifications")] +pub use app::PushToken; + pub(crate) mod app; pub mod async_runtime; mod error; From 2a41de01b7a2aecc9b35f915cb09c0377d3e4fcd Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Mon, 11 Nov 2024 16:56:22 -0800 Subject: [PATCH 2/9] chore: align versions Signed-off-by: Sam Gammon --- crates/tauri-runtime-wry/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/Cargo.toml b/crates/tauri-runtime-wry/Cargo.toml index 8e3efe4121ca..cecaa139e7d4 100644 --- a/crates/tauri-runtime-wry/Cargo.toml +++ b/crates/tauri-runtime-wry/Cargo.toml @@ -23,7 +23,7 @@ wry = { version = "0.47", default-features = false, features = [ "os-webview", "linux-body", ] } -tao = { version = "0.30.6", default-features = false, features = ["rwh_06"] } +tao = { version = "0.30.8", default-features = false, features = ["rwh_06"] } tauri-runtime = { version = "2.2.0", path = "../tauri-runtime" } tauri-utils = { version = "2.1.0", path = "../tauri-utils" } raw-window-handle = "0.6" From 9d620a8d4911d105f4a1119c4088d5e2ccdff88f Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Mon, 11 Nov 2024 16:59:14 -0800 Subject: [PATCH 3/9] chore: `tao` override for pr Signed-off-by: Sam Gammon --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 963894b5910b..155ee90adb86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,3 +70,4 @@ opt-level = "s" [patch.crates-io] schemars_derive = { git = 'https://github.com/tauri-apps/schemars.git', branch = 'feat/preserve-description-newlines' } tauri = { path = "./crates/tauri" } +tao = { git = "https://github.com/sgammon/tao.git", rev = "11854b206d1eb32aa88d542bb2f8e3f20eebbb4c" } From ecff1d2a1087b297b3b916eaba475a73a70646b9 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Mon, 11 Nov 2024 16:59:20 -0800 Subject: [PATCH 4/9] chore: update locks Signed-off-by: Sam Gammon --- Cargo.lock | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b97cf5f91a61..67cb6c859d76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4273,15 +4273,6 @@ dependencies = [ "similar", ] -[[package]] -name = "instant" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" -dependencies = [ - "cfg-if", -] - [[package]] name = "interpolate_name" version = "0.2.4" @@ -8921,9 +8912,8 @@ dependencies = [ [[package]] name = "tao" -version = "0.30.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "833b4d43383d76d5078d72f3acd977f47eb5b6751eb40baa665d13828e7b79df" +version = "0.30.8" +source = "git+https://github.com/sgammon/tao.git?rev=11854b206d1eb32aa88d542bb2f8e3f20eebbb4c#11854b206d1eb32aa88d542bb2f8e3f20eebbb4c" dependencies = [ "bitflags 2.6.0", "cocoa 0.26.0", @@ -8936,7 +8926,6 @@ dependencies = [ "gdkwayland-sys", "gdkx11-sys", "gtk", - "instant", "jni", "lazy_static", "libc", @@ -8949,7 +8938,7 @@ dependencies = [ "parking_lot", "raw-window-handle", "scopeguard", - "tao-macros", + "tao-macros 0.1.3 (git+https://github.com/sgammon/tao.git?rev=11854b206d1eb32aa88d542bb2f8e3f20eebbb4c)", "unicode-segmentation", "url", "windows", @@ -8969,6 +8958,16 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "tao-macros" +version = "0.1.3" +source = "git+https://github.com/sgammon/tao.git?rev=11854b206d1eb32aa88d542bb2f8e3f20eebbb4c#11854b206d1eb32aa88d542bb2f8e3f20eebbb4c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "tap" version = "1.0.1" @@ -11310,7 +11309,7 @@ dependencies = [ "raw-window-handle", "sha2", "soup3", - "tao-macros", + "tao-macros 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror 1.0.68", "tracing", "url", From 5f69869be0f024fbcbfdd0829967ae9fa560d5f0 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Fri, 15 Nov 2024 17:38:31 -0800 Subject: [PATCH 5/9] fixup! `PushToken` alias import Signed-off-by: Sam Gammon --- crates/tauri-runtime-wry/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 0d662a7cec8c..835023e65bb5 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -18,6 +18,7 @@ use http::Request; use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle}; use tauri_runtime::{ + PushToken, dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size}, monitor::Monitor, webview::{DetachedWebview, DownloadEvent, PendingWebview, WebviewIpcHandler}, From 4210599dfdf4c8dbe1984d778480fdbd2223232c Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Fri, 15 Nov 2024 23:30:56 -0800 Subject: [PATCH 6/9] fixup! toml fmt Signed-off-by: Sam Gammon --- crates/tauri/Cargo.toml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/tauri/Cargo.toml b/crates/tauri/Cargo.toml index 755be7341cf7..ec07d25bfae5 100644 --- a/crates/tauri/Cargo.toml +++ b/crates/tauri/Cargo.toml @@ -169,7 +169,10 @@ tray-icon = ["dep:tray-icon"] tracing = ["dep:tracing", "tauri-macros/tracing", "tauri-runtime-wry/tracing"] test = [] compression = ["tauri-macros/compression", "tauri-utils/compression"] -push-notifications = ["tauri-runtime/push-notifications", "tauri-runtime-wry/push-notifications"] +push-notifications = [ + "tauri-runtime/push-notifications", + "tauri-runtime-wry/push-notifications", +] wry = ["tauri-runtime-wry"] objc-exception = ["tauri-runtime-wry/objc-exception"] linux-libxdo = ["tray-icon/libxdo", "muda/libxdo"] From 328e4962a004b6b41001c294f8b548fbe9b22200 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Fri, 15 Nov 2024 23:31:16 -0800 Subject: [PATCH 7/9] chore: fmt Signed-off-by: Sam Gammon --- crates/tauri-runtime-wry/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 835023e65bb5..ca648ac8eaa1 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -18,7 +18,6 @@ use http::Request; use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle}; use tauri_runtime::{ - PushToken, dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size}, monitor::Monitor, webview::{DetachedWebview, DownloadEvent, PendingWebview, WebviewIpcHandler}, @@ -31,6 +30,9 @@ use tauri_runtime::{ UserEvent, WebviewDispatch, WebviewEventId, WindowDispatch, WindowEventId, }; +#[cfg(feature = "push-notifications")] +use tauri_runtime::PushToken; + #[cfg(any(target_os = "macos", target_os = "ios"))] use objc2::rc::Retained; #[cfg(target_os = "macos")] From 6ee678a586936d0efeb38bce0673152531b51386 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Fri, 15 Nov 2024 23:32:15 -0800 Subject: [PATCH 8/9] chore: latest `tao` fork Signed-off-by: Sam Gammon --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 155ee90adb86..b8caff7e75fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,4 +70,4 @@ opt-level = "s" [patch.crates-io] schemars_derive = { git = 'https://github.com/tauri-apps/schemars.git', branch = 'feat/preserve-description-newlines' } tauri = { path = "./crates/tauri" } -tao = { git = "https://github.com/sgammon/tao.git", rev = "11854b206d1eb32aa88d542bb2f8e3f20eebbb4c" } +tao = { git = "https://github.com/sgammon/tao.git", rev = "79cae516189f4a1253ff2956295ec3c9411a4383" } From d368d876d4f4582422bece76adea34b8a7982026 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Fri, 15 Nov 2024 23:48:12 -0800 Subject: [PATCH 9/9] docs: feature docs for `push-notifications` Signed-off-by: Sam Gammon --- crates/tauri/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/tauri/src/lib.rs b/crates/tauri/src/lib.rs index 4101b053e90b..5df69537e562 100644 --- a/crates/tauri/src/lib.rs +++ b/crates/tauri/src/lib.rs @@ -37,6 +37,7 @@ //! - **image-png**: Adds support to parse `.png` image, see [`Image`]. //! - **macos-proxy**: Adds support for [`WebviewBuilder::proxy_url`] on macOS. Requires macOS 14+. //! - **specta**: Add support for [`specta::specta`](https://docs.rs/specta/%5E2.0.0-rc.9/specta/attr.specta.html) with Tauri arguments such as [`State`](crate::State), [`Window`](crate::Window) and [`AppHandle`](crate::AppHandle) +//! - **push-notifications**: Add support for [Apple APNS](https://developer.apple.com/notifications/), [Windows WNS](https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/notifications/push-notifications/), and similar systems //! //! ## Cargo allowlist features //!