#![doc(
html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
)]
use serde::Serialize;
#[cfg(mobile)]
use tauri::plugin::PluginHandle;
#[cfg(desktop)]
use tauri::AppHandle;
use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
pub use models::*;
pub use tauri::plugin::PermissionState;
#[cfg(desktop)]
mod desktop;
#[cfg(mobile)]
mod mobile;
mod commands;
mod error;
mod models;
pub use error::{Error, Result};
#[cfg(desktop)]
pub use desktop::Notification;
#[cfg(mobile)]
pub use mobile::Notification;
#[derive(Debug)]
pub struct NotificationBuilder<R: Runtime> {
#[cfg(desktop)]
app: AppHandle<R>,
#[cfg(mobile)]
handle: PluginHandle<R>,
pub(crate) data: NotificationData,
}
impl<R: Runtime> NotificationBuilder<R> {
#[cfg(desktop)]
fn new(app: AppHandle<R>) -> Self {
Self {
app,
data: Default::default(),
}
}
#[cfg(mobile)]
fn new(handle: PluginHandle<R>) -> Self {
Self {
handle,
data: Default::default(),
}
}
pub fn id(mut self, id: i32) -> Self {
self.data.id = id;
self
}
pub fn channel_id(mut self, id: impl Into<String>) -> Self {
self.data.channel_id.replace(id.into());
self
}
pub fn title(mut self, title: impl Into<String>) -> Self {
self.data.title.replace(title.into());
self
}
pub fn body(mut self, body: impl Into<String>) -> Self {
self.data.body.replace(body.into());
self
}
pub fn schedule(mut self, schedule: Schedule) -> Self {
self.data.schedule.replace(schedule);
self
}
pub fn large_body(mut self, large_body: impl Into<String>) -> Self {
self.data.large_body.replace(large_body.into());
self
}
pub fn summary(mut self, summary: impl Into<String>) -> Self {
self.data.summary.replace(summary.into());
self
}
pub fn action_type_id(mut self, action_type_id: impl Into<String>) -> Self {
self.data.action_type_id.replace(action_type_id.into());
self
}
pub fn group(mut self, group: impl Into<String>) -> Self {
self.data.group.replace(group.into());
self
}
pub fn group_summary(mut self) -> Self {
self.data.group_summary = true;
self
}
pub fn sound(mut self, sound: impl Into<String>) -> Self {
self.data.sound.replace(sound.into());
self
}
pub fn inbox_line(mut self, line: impl Into<String>) -> Self {
self.data.inbox_lines.push(line.into());
self
}
pub fn icon(mut self, icon: impl Into<String>) -> Self {
self.data.icon.replace(icon.into());
self
}
pub fn large_icon(mut self, large_icon: impl Into<String>) -> Self {
self.data.large_icon.replace(large_icon.into());
self
}
pub fn icon_color(mut self, icon_color: impl Into<String>) -> Self {
self.data.icon_color.replace(icon_color.into());
self
}
pub fn attachment(mut self, attachment: Attachment) -> Self {
self.data.attachments.push(attachment);
self
}
pub fn extra(mut self, key: impl Into<String>, value: impl Serialize) -> Self {
self.data
.extra
.insert(key.into(), serde_json::to_value(value).unwrap());
self
}
pub fn ongoing(mut self) -> Self {
self.data.ongoing = true;
self
}
pub fn auto_cancel(mut self) -> Self {
self.data.auto_cancel = true;
self
}
pub fn silent(mut self) -> Self {
self.data.silent = true;
self
}
}
pub trait NotificationExt<R: Runtime> {
fn notification(&self) -> &Notification<R>;
}
impl<R: Runtime, T: Manager<R>> crate::NotificationExt<R> for T {
fn notification(&self) -> &Notification<R> {
self.state::<Notification<R>>().inner()
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("notification")
.invoke_handler(tauri::generate_handler![
commands::notify,
commands::request_permission,
commands::is_permission_granted
])
.js_init_script(include_str!("init-iife.js").replace(
"__TEMPLATE_windows__",
if cfg!(windows) { "true" } else { "false" },
))
.setup(|app, api| {
#[cfg(mobile)]
let notification = mobile::init(app, api)?;
#[cfg(desktop)]
let notification = desktop::init(app, api)?;
app.manage(notification);
Ok(())
})
.build()
}