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 self.data
177 .extra
178 .insert(key.into(), serde_json::to_value(value).unwrap());
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}