tauri_plugin_notification/
mobile.rs1use serde::{Deserialize, de::DeserializeOwned};
6use tauri::{
7 AppHandle, Runtime,
8 plugin::{PermissionState, PluginApi, PluginHandle},
9};
10
11use crate::models::*;
12
13use std::collections::HashMap;
14
15#[cfg(target_os = "android")]
16const PLUGIN_IDENTIFIER: &str = "app.tauri.notification";
17
18#[cfg(target_os = "ios")]
19tauri::ios_plugin_binding!(init_plugin_notification);
20
21pub fn init<R: Runtime, C: DeserializeOwned>(
24 _app: &AppHandle<R>,
25 api: PluginApi<R, C>,
26) -> crate::Result<Notification<R>> {
27 #[cfg(target_os = "android")]
28 let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "NotificationPlugin")?;
29 #[cfg(target_os = "ios")]
30 let handle = api.register_ios_plugin(init_plugin_notification)?;
31 Ok(Notification(handle))
32}
33
34impl<R: Runtime> crate::NotificationBuilder<R> {
35 pub fn show(self) -> crate::Result<()> {
42 self.handle
43 .run_mobile_plugin::<i32>("show", self.data)
44 .map(|_| ())
45 .map_err(Into::into)
46 }
47}
48
49pub struct Notification<R: Runtime>(PluginHandle<R>);
53
54impl<R: Runtime> Notification<R> {
55 pub fn builder(&self) -> crate::NotificationBuilder<R> {
72 crate::NotificationBuilder::new(self.0.clone())
73 }
74
75 pub fn request_permission(&self) -> crate::Result<PermissionState> {
79 self.0
80 .run_mobile_plugin::<PermissionResponse>("requestPermissions", ())
81 .map(|r| r.permission_state)
82 .map_err(Into::into)
83 }
84
85 pub fn permission_state(&self) -> crate::Result<PermissionState> {
87 self.0
88 .run_mobile_plugin::<PermissionResponse>("checkPermissions", ())
89 .map(|r| r.permission_state)
90 .map_err(Into::into)
91 }
92
93 pub fn register_action_types(&self, types: Vec<ActionType>) -> crate::Result<()> {
101 let mut args = HashMap::new();
102 args.insert("types", types);
103 self.0
104 .run_mobile_plugin("registerActionTypes", args)
105 .map_err(Into::into)
106 }
107
108 pub fn remove_active(&self, notifications: Vec<i32>) -> crate::Result<()> {
112 let mut args = HashMap::new();
113 args.insert(
114 "notifications",
115 notifications
116 .into_iter()
117 .map(|id| {
118 let mut notification = HashMap::new();
119 notification.insert("id", id);
120 notification
121 })
122 .collect::<Vec<HashMap<&str, i32>>>(),
123 );
124 self.0
125 .run_mobile_plugin("removeActive", args)
126 .map_err(Into::into)
127 }
128
129 pub fn active(&self) -> crate::Result<Vec<ActiveNotification>> {
131 self.0
132 .run_mobile_plugin("getActive", ())
133 .map_err(Into::into)
134 }
135
136 pub fn remove_all_active(&self) -> crate::Result<()> {
138 self.0
139 .run_mobile_plugin("removeActive", ())
140 .map_err(Into::into)
141 }
142
143 pub fn pending(&self) -> crate::Result<Vec<PendingNotification>> {
145 self.0
146 .run_mobile_plugin("getPending", ())
147 .map_err(Into::into)
148 }
149
150 pub fn cancel(&self, notifications: Vec<i32>) -> crate::Result<()> {
152 let mut args = HashMap::new();
153 args.insert("notifications", notifications);
154 self.0.run_mobile_plugin("cancel", args).map_err(Into::into)
155 }
156
157 pub fn cancel_all(&self) -> crate::Result<()> {
159 self.0.run_mobile_plugin("cancel", ()).map_err(Into::into)
160 }
161
162 #[cfg(target_os = "android")]
169 pub fn create_channel(&self, channel: Channel) -> crate::Result<()> {
170 self.0
171 .run_mobile_plugin("createChannel", channel)
172 .map_err(Into::into)
173 }
174
175 #[cfg(target_os = "android")]
179 pub fn delete_channel(&self, id: impl Into<String>) -> crate::Result<()> {
180 let mut args = HashMap::new();
181 args.insert("id", id.into());
182 self.0
183 .run_mobile_plugin("deleteChannel", args)
184 .map_err(Into::into)
185 }
186
187 #[cfg(target_os = "android")]
191 pub fn list_channels(&self) -> crate::Result<Vec<Channel>> {
192 self.0
193 .run_mobile_plugin("listChannels", ())
194 .map_err(Into::into)
195 }
196}
197
198#[derive(Deserialize)]
199#[serde(rename_all = "camelCase")]
200struct PermissionResponse {
201 permission_state: PermissionState,
202}