tauri_plugin_notifications/
lib.rs1use serde::Serialize;
8#[cfg(mobile)]
9use tauri::plugin::PluginHandle;
10#[cfg(desktop)]
11use tauri::AppHandle;
12use tauri::{
13 plugin::{Builder, TauriPlugin},
14 Manager, Runtime,
15};
16
17pub use models::*;
18pub use tauri::plugin::PermissionState;
19
20#[cfg(desktop)]
21mod desktop;
22#[cfg(mobile)]
23mod mobile;
24
25mod commands;
26mod error;
27mod models;
28
29pub use error::{Error, Result};
30
31#[cfg(desktop)]
32pub use desktop::Notifications;
33#[cfg(mobile)]
34pub use mobile::Notifications;
35
36#[derive(Debug)]
38pub struct NotificationsBuilder<R: Runtime> {
39 #[cfg(desktop)]
40 app: AppHandle<R>,
41 #[cfg(mobile)]
42 handle: PluginHandle<R>,
43 pub(crate) data: NotificationData,
44}
45
46impl<R: Runtime> NotificationsBuilder<R> {
47 #[cfg(desktop)]
48 fn new(app: AppHandle<R>) -> Self {
49 Self {
50 app,
51 data: Default::default(),
52 }
53 }
54
55 #[cfg(mobile)]
56 fn new(handle: PluginHandle<R>) -> Self {
57 Self {
58 handle,
59 data: Default::default(),
60 }
61 }
62
63 pub fn id(mut self, id: i32) -> Self {
65 self.data.id = id;
66 self
67 }
68
69 pub fn channel_id(mut self, id: impl Into<String>) -> Self {
74 self.data.channel_id.replace(id.into());
75 self
76 }
77
78 pub fn title(mut self, title: impl Into<String>) -> Self {
80 self.data.title.replace(title.into());
81 self
82 }
83
84 pub fn body(mut self, body: impl Into<String>) -> Self {
86 self.data.body.replace(body.into());
87 self
88 }
89
90 pub fn schedule(mut self, schedule: Schedule) -> Self {
92 self.data.schedule.replace(schedule);
93 self
94 }
95
96 pub fn large_body(mut self, large_body: impl Into<String>) -> Self {
100 self.data.large_body.replace(large_body.into());
101 self
102 }
103
104 pub fn summary(mut self, summary: impl Into<String>) -> Self {
106 self.data.summary.replace(summary.into());
107 self
108 }
109
110 pub fn action_type_id(mut self, action_type_id: impl Into<String>) -> Self {
112 self.data.action_type_id.replace(action_type_id.into());
113 self
114 }
115
116 pub fn group(mut self, group: impl Into<String>) -> Self {
120 self.data.group.replace(group.into());
121 self
122 }
123
124 pub fn group_summary(mut self) -> Self {
126 self.data.group_summary = true;
127 self
128 }
129
130 pub fn sound(mut self, sound: impl Into<String>) -> Self {
132 self.data.sound.replace(sound.into());
133 self
134 }
135
136 pub fn inbox_line(mut self, line: impl Into<String>) -> Self {
142 self.data.inbox_lines.push(line.into());
143 self
144 }
145
146 pub fn icon(mut self, icon: impl Into<String>) -> Self {
150 self.data.icon.replace(icon.into());
151 self
152 }
153
154 pub fn large_icon(mut self, large_icon: impl Into<String>) -> Self {
158 self.data.large_icon.replace(large_icon.into());
159 self
160 }
161
162 pub fn icon_color(mut self, icon_color: impl Into<String>) -> Self {
164 self.data.icon_color.replace(icon_color.into());
165 self
166 }
167
168 pub fn attachment(mut self, attachment: Attachment) -> Self {
170 self.data.attachments.push(attachment);
171 self
172 }
173
174 pub fn extra(mut self, key: impl Into<String>, value: impl Serialize) -> Self {
176 if let Ok(value) = serde_json::to_value(value) {
177 self.data.extra.insert(key.into(), value);
178 }
179 self
180 }
181
182 pub fn ongoing(mut self) -> Self {
188 self.data.ongoing = true;
189 self
190 }
191
192 pub fn auto_cancel(mut self) -> Self {
194 self.data.auto_cancel = true;
195 self
196 }
197
198 pub fn silent(mut self) -> Self {
200 self.data.silent = true;
201 self
202 }
203}
204
205pub trait NotificationsExt<R: Runtime> {
207 fn notifications(&self) -> &Notifications<R>;
208}
209
210impl<R: Runtime, T: Manager<R>> crate::NotificationsExt<R> for T {
211 fn notifications(&self) -> &Notifications<R> {
212 self.state::<Notifications<R>>().inner()
213 }
214}
215
216pub fn init<R: Runtime>() -> TauriPlugin<R> {
218 Builder::new("notifications")
219 .invoke_handler(tauri::generate_handler![
220 commands::notify,
221 commands::request_permission,
222 commands::register_for_push_notifications,
223 commands::is_permission_granted,
224 ])
225 .setup(|app, api| {
226 #[cfg(mobile)]
227 let notification = mobile::init(app, api)?;
228 #[cfg(desktop)]
229 let notification = desktop::init(app, api)?;
230 app.manage(notification);
231 Ok(())
232 })
233 .build()
234}
235
236#[cfg(test)]
237mod tests {
238 use super::*;
239
240 #[cfg(desktop)]
242 fn create_test_data() -> NotificationData {
243 NotificationData::default()
244 }
245
246 #[cfg(mobile)]
247 fn create_test_data() -> NotificationData {
248 NotificationData::default()
249 }
250
251 #[test]
252 fn test_notification_data_id() {
253 let mut data = create_test_data();
254 data.id = 42;
255 assert_eq!(data.id, 42);
256 }
257
258 #[test]
259 fn test_notification_data_channel_id() {
260 let mut data = create_test_data();
261 data.channel_id = Some("test_channel".to_string());
262 assert_eq!(data.channel_id, Some("test_channel".to_string()));
263 }
264
265 #[test]
266 fn test_notification_data_title() {
267 let mut data = create_test_data();
268 data.title = Some("Test Title".to_string());
269 assert_eq!(data.title, Some("Test Title".to_string()));
270 }
271
272 #[test]
273 fn test_notification_data_body() {
274 let mut data = create_test_data();
275 data.body = Some("Test Body".to_string());
276 assert_eq!(data.body, Some("Test Body".to_string()));
277 }
278
279 #[test]
280 fn test_notification_data_large_body() {
281 let mut data = create_test_data();
282 data.large_body = Some("Large Body Text".to_string());
283 assert_eq!(data.large_body, Some("Large Body Text".to_string()));
284 }
285
286 #[test]
287 fn test_notification_data_summary() {
288 let mut data = create_test_data();
289 data.summary = Some("Summary Text".to_string());
290 assert_eq!(data.summary, Some("Summary Text".to_string()));
291 }
292
293 #[test]
294 fn test_notification_data_action_type_id() {
295 let mut data = create_test_data();
296 data.action_type_id = Some("action_type".to_string());
297 assert_eq!(data.action_type_id, Some("action_type".to_string()));
298 }
299
300 #[test]
301 fn test_notification_data_group() {
302 let mut data = create_test_data();
303 data.group = Some("test_group".to_string());
304 assert_eq!(data.group, Some("test_group".to_string()));
305 }
306
307 #[test]
308 fn test_notification_data_group_summary() {
309 let mut data = create_test_data();
310 data.group_summary = true;
311 assert!(data.group_summary);
312 }
313
314 #[test]
315 fn test_notification_data_sound() {
316 let mut data = create_test_data();
317 data.sound = Some("notification_sound".to_string());
318 assert_eq!(data.sound, Some("notification_sound".to_string()));
319 }
320
321 #[test]
322 fn test_notification_data_inbox_lines() {
323 let mut data = create_test_data();
324 data.inbox_lines.push("Line 1".to_string());
325 data.inbox_lines.push("Line 2".to_string());
326 assert_eq!(data.inbox_lines.len(), 2);
327 assert_eq!(data.inbox_lines[0], "Line 1");
328 assert_eq!(data.inbox_lines[1], "Line 2");
329 }
330
331 #[test]
332 fn test_notification_data_icon() {
333 let mut data = create_test_data();
334 data.icon = Some("icon_name".to_string());
335 assert_eq!(data.icon, Some("icon_name".to_string()));
336 }
337
338 #[test]
339 fn test_notification_data_large_icon() {
340 let mut data = create_test_data();
341 data.large_icon = Some("large_icon_name".to_string());
342 assert_eq!(data.large_icon, Some("large_icon_name".to_string()));
343 }
344
345 #[test]
346 fn test_notification_data_icon_color() {
347 let mut data = create_test_data();
348 data.icon_color = Some("#FF0000".to_string());
349 assert_eq!(data.icon_color, Some("#FF0000".to_string()));
350 }
351
352 #[test]
353 fn test_notification_data_attachments() {
354 let mut data = create_test_data();
355 let url = url::Url::parse("https://example.com/image.png").expect("Failed to parse URL");
356 let attachment = Attachment::new("attachment1", url);
357 data.attachments.push(attachment);
358 assert_eq!(data.attachments.len(), 1);
359 }
360
361 #[test]
362 fn test_notification_data_extra() {
363 let mut data = create_test_data();
364 data.extra
365 .insert("key1".to_string(), serde_json::json!("value1"));
366 data.extra.insert("key2".to_string(), serde_json::json!(42));
367 assert_eq!(data.extra.len(), 2);
368 assert_eq!(data.extra.get("key1"), Some(&serde_json::json!("value1")));
369 assert_eq!(data.extra.get("key2"), Some(&serde_json::json!(42)));
370 }
371
372 #[test]
373 fn test_notification_data_ongoing() {
374 let mut data = create_test_data();
375 data.ongoing = true;
376 assert!(data.ongoing);
377 }
378
379 #[test]
380 fn test_notification_data_auto_cancel() {
381 let mut data = create_test_data();
382 data.auto_cancel = true;
383 assert!(data.auto_cancel);
384 }
385
386 #[test]
387 fn test_notification_data_silent() {
388 let mut data = create_test_data();
389 data.silent = true;
390 assert!(data.silent);
391 }
392
393 #[test]
394 fn test_notification_data_schedule() {
395 let mut data = create_test_data();
396 let schedule = Schedule::Every {
397 interval: ScheduleEvery::Day,
398 count: 1,
399 allow_while_idle: false,
400 };
401 data.schedule = Some(schedule);
402 assert!(data.schedule.is_some());
403 assert!(matches!(data.schedule, Some(Schedule::Every { .. })));
404 }
405}