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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! 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, block_on_main, 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: use block_on_main to drive the run loop while waiting for the response
//! let response = block_on_main(async {
//! 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()
//! .await?
//! .response()
//! .await
//! }).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, to enable async APIs.
//!
//! ## GUI apps (`AppKit` / `SwiftUI` / `Tauri`)
//!
//! The framework drives the main run loop automatically. `send`, `send_blocking`, and
//! `response().await` all work from any thread without extra setup.
//!
//! ## Headless / background-only apps
//!
//! No framework drives the run loop, so you must do it yourself. Use [`block_on_main`]
//! to run an async expression on the main thread while pumping the run loop:
//!
//! ```no_run
//! # use mac_usernotifications::{Notification, block_on_main};
//! # fn main() {
//! let response = block_on_main(async {
//! Notification::new()
//! .title("Hello")
//! .send().await?
//! .response().await
//! });
//! # }
//! ```
//!
//! Or for Tokio, keep the main thread free for [`run_main_loop_while`] and run the
//! runtime entirely on background threads. See `examples/simple_tokio.rs`.
//!
//! ## Known pitfalls
//!
//! | Scenario | Symptom | Fix |
//! |---|---|---|
//! | `response().await` called with nothing pumping the main run loop | future never resolves | Use [`block_on_main`] or [`run_main_loop_while`] on the main thread |
//! | `#[tokio::main]` — main thread is inside Tokio | callbacks never fire | Use `new_multi_thread()` and keep main free for [`run_main_loop_while`] |
//! | User clicks **"Clear All"** in Notification Center | future never resolves | Always set a timeout via [`Notification::timeout`] for actionable notifications |
use ;
use Future;
pub use crate::;
pub use block_on;
/// Set the application which delivers or schedules a notification
/// A [`RawWakerVTable`](std::task::RawWakerVTable) whose `wake` calls [`CFRunLoop::wake_up`](objc2_core_foundation::CFRunLoop::wake_up) on the main run loop.
///
/// The data pointer is always null; the main run loop is a global.
/// Run a future to completion on the main thread while pumping [`NSRunLoop`](https://developer.apple.com/documentation/foundation/nsrunloop).
///
/// ## Thread Safety
/// **Must be called from the main thread.**
/// Uses a waker that calls [`CFRunLoop::wake_up`](objc2_core_foundation::CFRunLoop::wake_up) on the main run loop,
/// so the run loop sleep is interrupted as soon as the future signals readiness, no busy-polling.
/// GUI apps (`Tauri`, `AppKit`, `SwiftUI`) pump [`RunLoop`](https://developer.apple.com/documentation/foundation/runloop) automatically.
///
/// **Use [`dynamic_block_on`] instead if the main thread may not be running.**
/// Block on the future, using [`block_on_main`] if the current thread is the main thread.
///
/// Returns `None` if the main thread is not pumping.
/// Returns `true` if the main thread's run loop is active and able to deliver callbacks.
///
/// macOS always delivers notification responses on the main thread's run loop.
/// If nothing is running it (no AppKit/SwiftUI framework, no explicit [`run_main_loop_while`] call),
/// response futures will never resolve.
pub
/// Verify the process has a bundle identifier.
///
/// [`UNUserNotificationCenter`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter) requires this and crashes without it.