mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-21 11:26:15 +02:00
67 lines
1.9 KiB
Rust
67 lines
1.9 KiB
Rust
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
use serde::{de::DeserializeOwned, Deserialize};
|
|
use tauri::{
|
|
plugin::{PluginApi, PluginHandle},
|
|
AppHandle, Runtime,
|
|
};
|
|
|
|
use crate::models::*;
|
|
|
|
#[cfg(target_os = "android")]
|
|
const PLUGIN_IDENTIFIER: &str = "app.tauri.notification";
|
|
|
|
#[cfg(target_os = "ios")]
|
|
tauri::ios_plugin_binding!(init_plugin_notification);
|
|
|
|
// initializes the Kotlin or Swift plugin classes
|
|
pub fn init<R: Runtime, C: DeserializeOwned>(
|
|
_app: &AppHandle<R>,
|
|
api: PluginApi<R, C>,
|
|
) -> crate::Result<Notification<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(Notification(handle))
|
|
}
|
|
|
|
impl<R: Runtime> crate::NotificationBuilder<R> {
|
|
pub fn show(self) -> crate::Result<()> {
|
|
self.handle
|
|
.run_mobile_plugin("notify", self.data)
|
|
.map_err(Into::into)
|
|
}
|
|
}
|
|
|
|
/// Access to the notification APIs.
|
|
pub struct Notification<R: Runtime>(PluginHandle<R>);
|
|
|
|
impl<R: Runtime> Notification<R> {
|
|
pub fn builder(&self) -> crate::NotificationBuilder<R> {
|
|
crate::NotificationBuilder::new(self.0.clone())
|
|
}
|
|
|
|
pub fn request_permission(&self) -> crate::Result<PermissionState> {
|
|
self.0
|
|
.run_mobile_plugin::<PermissionResponse>("requestPermission", ())
|
|
.map(|r| r.permission_state)
|
|
.map_err(Into::into)
|
|
}
|
|
|
|
pub fn permission_state(&self) -> crate::Result<PermissionState> {
|
|
self.0
|
|
.run_mobile_plugin::<PermissionResponse>("permissionState", ())
|
|
.map(|r| r.permission_state)
|
|
.map_err(Into::into)
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct PermissionResponse {
|
|
permission_state: PermissionState,
|
|
}
|