use serde::de::DeserializeOwned;
use tauri::{
plugin::{PermissionState, PluginApi, PluginHandle},
AppHandle, Runtime,
};
use crate::models::*;
use std::collections::HashMap;
#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "app.tauri.notification";
#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_notification);
pub fn init<R: Runtime, C: DeserializeOwned>(
_app: &AppHandle<R>,
api: PluginApi<R, C>,
) -> crate::Result<Notifications<R>> {
#[cfg(target_os = "android")]
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "NotificationPlugin")?;
#[cfg(target_os = "ios")]
let handle = api.register_ios_plugin(init_plugin_notification)?;
Ok(Notifications(handle))
}
impl<R: Runtime> crate::NotificationsBuilder<R> {
pub async fn show(self) -> crate::Result<()> {
self.handle
.run_mobile_plugin_async::<i32>("show", self.data)
.await
.map(|_| ())
.map_err(Into::into)
}
}
pub struct Notifications<R: Runtime>(PluginHandle<R>);
impl<R: Runtime> Notifications<R> {
pub fn builder(&self) -> crate::NotificationsBuilder<R> {
crate::NotificationsBuilder::new(self.0.clone())
}
pub async fn request_permission(&self) -> crate::Result<PermissionState> {
self.0
.run_mobile_plugin_async::<PermissionResponse>("requestPermissions", ())
.await
.map(|r| r.permission_state)
.map_err(Into::into)
}
pub async fn register_for_push_notifications(&self) -> crate::Result<String> {
#[cfg(feature = "push-notifications")]
{
self.0
.run_mobile_plugin_async::<PushNotificationResponse>(
"registerForPushNotifications",
(),
)
.await
.map(|r| r.device_token)
.map_err(Into::into)
}
#[cfg(not(feature = "push-notifications"))]
{
Err(crate::Error::Io(std::io::Error::other(
"Push notifications feature is not enabled",
)))
}
}
pub fn unregister_for_push_notifications(&self) -> crate::Result<()> {
#[cfg(feature = "push-notifications")]
{
self.0
.run_mobile_plugin::<()>("unregisterForPushNotifications", ())
.map_err(Into::into)
}
#[cfg(not(feature = "push-notifications"))]
{
Err(crate::Error::Io(std::io::Error::other(
"Push notifications feature is not enabled",
)))
}
}
pub async fn permission_state(&self) -> crate::Result<PermissionState> {
self.0
.run_mobile_plugin_async::<PermissionResponse>("checkPermissions", ())
.await
.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 async fn active(&self) -> crate::Result<Vec<ActiveNotification>> {
self.0
.run_mobile_plugin_async("getActive", ())
.await
.map_err(Into::into)
}
pub fn remove_all_active(&self) -> crate::Result<()> {
self.0
.run_mobile_plugin("removeActive", ())
.map_err(Into::into)
}
pub async fn pending(&self) -> crate::Result<Vec<PendingNotification>> {
self.0
.run_mobile_plugin_async("getPending", ())
.await
.map_err(Into::into)
}
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)
}
pub fn cancel_all(&self) -> crate::Result<()> {
self.0
.run_mobile_plugin("cancelAll", ())
.map_err(Into::into)
}
#[allow(unused_variables)]
pub fn create_channel(&self, channel: Channel) -> crate::Result<()> {
#[cfg(target_os = "android")]
return self
.0
.run_mobile_plugin("createChannel", channel)
.map_err(Into::into);
#[cfg(target_os = "ios")]
return Err(crate::Error::Io(std::io::Error::other(
"Channels are not supported on iOS",
)));
}
#[allow(unused_variables)]
pub fn delete_channel(&self, id: impl Into<String>) -> crate::Result<()> {
#[cfg(target_os = "android")]
{
let mut args = HashMap::new();
args.insert("id", id.into());
self.0
.run_mobile_plugin("deleteChannel", args)
.map_err(Into::into)
}
#[cfg(target_os = "ios")]
return Err(crate::Error::Io(std::io::Error::other(
"Channels are not supported on iOS",
)));
}
pub fn list_channels(&self) -> crate::Result<Vec<Channel>> {
#[cfg(target_os = "android")]
return self
.0
.run_mobile_plugin("listChannels", ())
.map_err(Into::into);
#[cfg(target_os = "ios")]
return Err(crate::Error::Io(std::io::Error::other(
"Channels are not supported on iOS",
)));
}
pub fn set_click_listener_active(&self, active: bool) -> crate::Result<()> {
let mut args = HashMap::new();
args.insert("active", active);
self.0
.run_mobile_plugin("setClickListenerActive", args)
.map_err(Into::into)
}
}