tauri_plugin_notification/
mobile.rs1use serde::{Deserialize, de::DeserializeOwned};
6use tauri::{
7 AppHandle, Runtime,
8 ipc::{Channel as IpcChannel, InvokeResponseBody},
9 plugin::{PermissionState, PluginApi, PluginHandle},
10};
11
12use crate::models::*;
13
14use std::collections::HashMap;
15
16#[cfg(target_os = "android")]
17const PLUGIN_IDENTIFIER: &str = "app.tauri.notification";
18
19#[cfg(target_os = "ios")]
20tauri::ios_plugin_binding!(init_plugin_notification);
21
22pub fn init<R: Runtime, C: DeserializeOwned>(
25 _app: &AppHandle<R>,
26 api: PluginApi<R, C>,
27) -> crate::Result<Notification<R>> {
28 #[cfg(target_os = "android")]
29 let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "NotificationPlugin")?;
30 #[cfg(target_os = "ios")]
31 let handle = api.register_ios_plugin(init_plugin_notification)?;
32 Ok(Notification(handle))
33}
34
35impl<R: Runtime> crate::NotificationBuilder<R> {
36 pub fn show(self) -> crate::Result<()> {
43 self.handle
44 .run_mobile_plugin::<i32>("show", self.data)
45 .map(|_| ())
46 .map_err(Into::into)
47 }
48}
49
50pub struct Notification<R: Runtime>(PluginHandle<R>);
54
55impl<R: Runtime> Notification<R> {
56 pub fn builder(&self) -> crate::NotificationBuilder<R> {
73 crate::NotificationBuilder::new(self.0.clone())
74 }
75
76 pub fn request_permission(&self) -> crate::Result<PermissionState> {
80 self.0
81 .run_mobile_plugin::<PermissionResponse>("requestPermissions", ())
82 .map(|r| r.permission_state)
83 .map_err(Into::into)
84 }
85
86 pub fn permission_state(&self) -> crate::Result<PermissionState> {
88 self.0
89 .run_mobile_plugin::<PermissionResponse>("checkPermissions", ())
90 .map(|r| r.permission_state)
91 .map_err(Into::into)
92 }
93
94 pub fn register_action_types(&self, types: Vec<ActionType>) -> crate::Result<()> {
102 let mut args = HashMap::new();
103 args.insert("types", types);
104 self.0
105 .run_mobile_plugin("registerActionTypes", args)
106 .map_err(Into::into)
107 }
108
109 pub fn on_action<F: Fn(&ActionPerformed) + Send + Sync + 'static>(
116 &self,
117 handler: F,
118 ) -> crate::Result<()> {
119 #[derive(serde::Serialize)]
120 struct RegisterListener {
121 event: &'static str,
122 handler: IpcChannel,
123 }
124 let channel = IpcChannel::new(move |body| {
125 if let InvokeResponseBody::Json(payload) = body
126 && let Ok(performed) = serde_json::from_str::<ActionPerformed>(&payload)
127 {
128 handler(&performed);
129 }
130 Ok(())
131 });
132 self.0
133 .run_mobile_plugin::<()>(
134 "registerListener",
135 RegisterListener {
136 event: "actionPerformed",
137 handler: channel,
138 },
139 )
140 .map_err(Into::into)
141 }
142
143 pub fn remove_active(&self, notifications: Vec<i32>) -> crate::Result<()> {
147 let mut args = HashMap::new();
148 args.insert(
149 "notifications",
150 notifications
151 .into_iter()
152 .map(|id| {
153 let mut notification = HashMap::new();
154 notification.insert("id", id);
155 notification
156 })
157 .collect::<Vec<HashMap<&str, i32>>>(),
158 );
159 self.0
160 .run_mobile_plugin("removeActive", args)
161 .map_err(Into::into)
162 }
163
164 pub fn active(&self) -> crate::Result<Vec<ActiveNotification>> {
166 self.0
167 .run_mobile_plugin("getActive", ())
168 .map_err(Into::into)
169 }
170
171 pub fn remove_all_active(&self) -> crate::Result<()> {
173 self.0
174 .run_mobile_plugin("removeActive", ())
175 .map_err(Into::into)
176 }
177
178 pub fn pending(&self) -> crate::Result<Vec<PendingNotification>> {
180 self.0
181 .run_mobile_plugin("getPending", ())
182 .map_err(Into::into)
183 }
184
185 pub fn cancel(&self, notifications: Vec<i32>) -> crate::Result<()> {
187 let mut args = HashMap::new();
188 args.insert("notifications", notifications);
189 self.0.run_mobile_plugin("cancel", args).map_err(Into::into)
190 }
191
192 pub fn cancel_all(&self) -> crate::Result<()> {
194 self.0.run_mobile_plugin("cancel", ()).map_err(Into::into)
195 }
196
197 #[cfg(target_os = "android")]
204 pub fn create_channel(&self, channel: Channel) -> crate::Result<()> {
205 self.0
206 .run_mobile_plugin("createChannel", channel)
207 .map_err(Into::into)
208 }
209
210 #[cfg(target_os = "android")]
214 pub fn delete_channel(&self, id: impl Into<String>) -> crate::Result<()> {
215 let mut args = HashMap::new();
216 args.insert("id", id.into());
217 self.0
218 .run_mobile_plugin("deleteChannel", args)
219 .map_err(Into::into)
220 }
221
222 #[cfg(target_os = "android")]
226 pub fn list_channels(&self) -> crate::Result<Vec<Channel>> {
227 self.0
228 .run_mobile_plugin("listChannels", ())
229 .map_err(Into::into)
230 }
231}
232
233#[derive(Deserialize)]
234#[serde(rename_all = "camelCase")]
235struct PermissionResponse {
236 permission_state: PermissionState,
237}