feat(notification): implement Android and iOS APIs (#340)

This commit is contained in:
Lucas Fernandes Nogueira
2023-05-05 05:22:19 -07:00
committed by GitHub
parent 1397172e95
commit be1c775b8d
25 changed files with 3700 additions and 90 deletions
+110 -3
View File
@@ -10,6 +10,8 @@ use tauri::{
use crate::models::*;
use std::collections::HashMap;
#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "app.tauri.notification";
@@ -31,7 +33,8 @@ pub fn init<R: Runtime, C: DeserializeOwned>(
impl<R: Runtime> crate::NotificationBuilder<R> {
pub fn show(self) -> crate::Result<()> {
self.handle
.run_mobile_plugin("notify", self.data)
.run_mobile_plugin::<ShowResponse>("show", self.data)
.map(|_| ())
.map_err(Into::into)
}
}
@@ -46,17 +49,121 @@ impl<R: Runtime> Notification<R> {
pub fn request_permission(&self) -> crate::Result<PermissionState> {
self.0
.run_mobile_plugin::<PermissionResponse>("requestPermission", ())
.run_mobile_plugin::<PermissionResponse>("requestPermissions", ())
.map(|r| r.permission_state)
.map_err(Into::into)
}
pub fn permission_state(&self) -> crate::Result<PermissionState> {
self.0
.run_mobile_plugin::<PermissionResponse>("permissionState", ())
.run_mobile_plugin::<PermissionResponse>("checkPermissions", ())
.map(|r| r.permission_state)
.map_err(Into::into)
}
pub fn register_action_types(&self, types: Vec<ActionType>) -> crate::Result<()> {
let mut args = HashMap::new();
args.insert("types", types);
self.0
.run_mobile_plugin("registerActionTypes", args)
.map_err(Into::into)
}
pub fn remove_active(&self, notifications: Vec<i32>) -> crate::Result<()> {
let mut args = HashMap::new();
args.insert(
"notifications",
notifications
.into_iter()
.map(|id| {
let mut notification = HashMap::new();
notification.insert("id", id);
notification
})
.collect::<Vec<HashMap<&str, i32>>>(),
);
self.0
.run_mobile_plugin("removeActive", args)
.map_err(Into::into)
}
pub fn active(&self) -> crate::Result<Vec<ActiveNotification>> {
self.0
.run_mobile_plugin::<ActiveResponse>("getActive", ())
.map(|r| r.notifications)
.map_err(Into::into)
}
pub fn remove_all_active(&self) -> crate::Result<()> {
self.0
.run_mobile_plugin("removeActive", ())
.map_err(Into::into)
}
pub fn pending(&self) -> crate::Result<Vec<PendingNotification>> {
self.0
.run_mobile_plugin::<PendingResponse>("getPending", ())
.map(|r| r.notifications)
.map_err(Into::into)
}
/// Cancel pending notifications.
pub fn cancel(&self, notifications: Vec<i32>) -> crate::Result<()> {
let mut args = HashMap::new();
args.insert("notifications", notifications);
self.0.run_mobile_plugin("cancel", args).map_err(Into::into)
}
/// Cancel all pending notifications.
pub fn cancel_all(&self) -> crate::Result<()> {
self.0.run_mobile_plugin("cancel", ()).map_err(Into::into)
}
#[cfg(target_os = "android")]
pub fn create_channel(&self, channel: Channel) -> crate::Result<()> {
self.0
.run_mobile_plugin("createChannel", channel)
.map_err(Into::into)
}
#[cfg(target_os = "android")]
pub fn delete_channel(&self, id: impl Into<String>) -> crate::Result<()> {
let mut args = HashMap::new();
args.insert("id", id.into());
self.0
.run_mobile_plugin("deleteChannel", args)
.map_err(Into::into)
}
#[cfg(target_os = "android")]
pub fn list_channels(&self) -> crate::Result<Vec<Channel>> {
self.0
.run_mobile_plugin::<ListChannelsResult>("listChannels", ())
.map(|r| r.channels)
.map_err(Into::into)
}
}
#[cfg(target_os = "android")]
#[derive(Deserialize)]
struct ListChannelsResult {
channels: Vec<Channel>,
}
#[derive(Deserialize)]
struct PendingResponse {
notifications: Vec<PendingNotification>,
}
#[derive(Deserialize)]
struct ActiveResponse {
notifications: Vec<ActiveNotification>,
}
#[derive(Deserialize)]
struct ShowResponse {
#[allow(dead_code)]
id: i32,
}
#[derive(Deserialize)]