1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! A Rust wrapper around [`UNUserNotificationCenter`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter),
//! designed for use in [notify-rust](https://docs.rs/notify-rust).
//!
//! # Bundling Requirement
//!
//! Contrary to [mac-notification-sys](https://docs.rs/mac-notification-sys),
//! this crate requires that the binary is bundled and be code-signed,
//! an ad-hoc signature is sufficient.
//! See the bundled examples for how to set this up with `cargo-bundle`.
//!
//! # Quick start
//!
//! ```no_run
//! # use mac_usernotifications::{Action, blocking, Notification, check_bundle};
//! # use std::time::Duration;
//! # fn main() {
//! // 1. verify the process has a bundle identifier
//! check_bundle().unwrap();
//!
//! // 2. verify user gave permission
//! blocking::request_auth().unwrap();
//!
//! // 3a. fire-and-forgeta (handle.notification_id() has the UUID for later use)
//! let handle = Notification::new()
//! .title("Hello")
//! .message("World")
//! .send_blocking()
//! .unwrap();
//!
//! println!("notification id: {}", handle.notification_id());
//!
//! // 3b. actionable: blocks until the user responds (use send().await in async contexts)
//! let response = Notification::new()
//! .title("Pick one")
//! .action(Action::button("ok", "OK"))
//! .action(Action::button("cancel", "Cancel"))
//! .timeout(Duration::from_secs(30)) // 4. always set a timeout for actionable notifications
//! .send_blocking()
//! .and_then(|handle| handle.response_blocking())
//! .unwrap();
//!
//! println!("User chose: {}", response.action_identifier);
//! # }
//! ```
//!
//! # Threading model
//!
//! macOS delivers [`didReceiveNotificationResponse`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/usernotificationcenter(_:didreceive:withcompletionhandler:)) on the main thread's [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop),
//! regardless of which thread the delegate was registered from ([Apple docs](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate)).
//! The main thread's run loop must be spinning whenever you expect the user to interact with a notification.
//!
//! This crate uses a lazily-created worker thread for all Objective-C calls.
//! That thread pumps its own [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop), but response callbacks still arrive on the **main** thread.
//!
//! ## GUI apps (`AppKit` / `SwiftUI` / Tauri)
//!
//! The framework drives the main run loop automatically. Both `send` and `send_blocking` work from any thread without extra setup.
//!
//! ## CLI tools
//!
//! Nothing pumps the main run loop by default, so you have to do it yourself.
//!
//! **Blocking:** `send_blocking` handles this automatically when called from
//! the main thread. It pumps [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop) between polls via [`block_on_main`].
//! Called from a background thread, it parks the caller and expects the main
//! run loop to be driven externally. See `examples/actions_blocking.rs`.
//!
//! **Async with Tokio:** `#[tokio::main]` occupies the main thread inside
//! Tokio's scheduler, so [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop) never runs and callbacks never fire.
//! Keep the main thread free and run Tokio on background threads instead:
//!
//! ```no_run
//! # use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
//! # fn main() {
//! // Multi-thread runtime lives entirely on background threads.
//! let rt = tokio::runtime::Builder::new_multi_thread()
//! .enable_all()
//! .build()
//! .unwrap();
//!
//! let done = Arc::new(AtomicBool::new(false));
//! let done2 = done.clone();
//!
//! rt.spawn(async move {
//! // ... your async code, using send() etc. ...
//! done2.store(true, Ordering::Release);
//! });
//!
//! // Main thread pumps NSRunLoop until async work signals completion.
//! mac_usernotifications::run_main_loop_while(|| !done.load(Ordering::Acquire));
//! # }
//! ```
//!
//! See `examples/simple_tokio.rs` for a complete working example.
//!
//! ## "Clear All" caveat
//!
//! If the user clicks **"Clear All"** in Notification Center,
//! [`didReceiveNotificationResponse`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/usernotificationcenter(_:didreceive:withcompletionhandler:)) is never called and the future will never
//! resolve. Always set a timeout via [`Notification::timeout`] for actionable
//! notifications.
use ;
use Future;
pub
pub use crate::;
pub use block_on;
/// Pump the main thread's [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop) until `should_continue` returns `false`.
///
/// **Must be called from the main thread.** Required because [`UNUserNotificationCenter`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter)
/// always delivers callbacks on the main thread's run loop; async runtimes that occupy
/// the main thread will never fire callbacks.
/// Run a future to completion on the main thread while pumping [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop).
///
/// **Must be called from the main thread.** Polls the future with a no-op waker,
/// pumping the main [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop) between polls to allow callbacks to fire.
/// GUI apps (Tauri, `AppKit`, `SwiftUI`) pump [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop) automatically; CLI tools need this.
/// Verify the process has a bundle identifier.
///
/// [`UNUserNotificationCenter`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter) requires this and crashes without it.