Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: push notifications #11652

Draft
wants to merge 9 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "79cae516189f4a1253ff2956295ec3c9411a4383" }
3 changes: 2 additions & 1 deletion crates/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,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")]
Expand Down Expand Up @@ -1351,6 +1354,10 @@ pub enum Message<T: 'static> {
Box<dyn FnOnce() -> (String, TaoWindowBuilder) + Send>,
Sender<Result<Weak<Window>>>,
),
#[cfg(feature = "push-notifications")]
PushRegistration(PushToken),
#[cfg(feature = "push-notifications")]
PushRegistrationFailed(String),
UserEvent(T),
}

Expand Down Expand Up @@ -3551,6 +3558,11 @@ fn handle_user_message<T: UserEvent>(
}
}

#[cfg(feature = "push-notifications")]
Message::PushRegistration(_) => (),
#[cfg(feature = "push-notifications")]
Message::PushRegistrationFailed(_) => (),

Message::UserEvent(_) => (),
Message::EventLoopWindowTarget(message) => match message {
EventLoopWindowTargetMessage::CursorPosition(sender) => {
Expand Down Expand Up @@ -3805,6 +3817,11 @@ fn handle_event_loop<T: UserEvent>(
} => 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)),

_ => (),
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ url = "2"
[features]
devtools = []
macos-private-api = []
push-notifications = []
7 changes: 7 additions & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub use dpi;

pub type WindowEventId = u32;
pub type WebviewEventId = u32;
pub type PushToken = Vec<u8>;

/// A rectangular region.
#[derive(Clone, Copy, Debug, Serialize)]
Expand Down Expand Up @@ -248,6 +249,12 @@ pub enum RunEvent<T: UserEvent> {
/// 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),
}
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +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",
]
wry = ["tauri-runtime-wry"]
objc-exception = ["tauri-runtime-wry/objc-exception"]
linux-libxdo = ["tray-icon/libxdo", "muda/libxdo"]
Expand Down
14 changes: 14 additions & 0 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub type OnPageLoad<R> = dyn Fn(&Webview<R>, &PageLoadPayload<'_>) + Send + Sync
pub type ChannelInterceptor<R> =
Box<dyn Fn(&Webview<R>, CallbackFn, usize, &InvokeResponseBody) -> bool + Send + Sync + 'static>;

/// Push notifications token type.
#[cfg(feature = "push-notifications")]
pub type PushToken = Vec<u8>;

/// The exit code on [`RunEvent::ExitRequested`] when [`AppHandle#method.restart`] is called.
pub const RESTART_EXIT_CODE: i32 = i32::MAX;

Expand Down Expand Up @@ -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<EventLoopMessage> for RunEvent {
Expand Down Expand Up @@ -2201,6 +2211,10 @@ fn on_event_loop_event<R: Runtime>(
}
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)]
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down Expand Up @@ -79,6 +80,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;
Expand Down
Loading