mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-08 14:03:53 +02:00
feat(notification): implement Android and iOS APIs (#340)
This commit is contained in:
committed by
GitHub
parent
1397172e95
commit
be1c775b8d
@@ -2,30 +2,21 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::Deserialize;
|
||||
use tauri::{command, AppHandle, Runtime, State};
|
||||
|
||||
use crate::{Notification, PermissionState, Result};
|
||||
|
||||
/// The options for the notification API.
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct NotificationOptions {
|
||||
/// The notification title.
|
||||
pub title: String,
|
||||
/// The notification body.
|
||||
pub body: Option<String>,
|
||||
/// The notification icon.
|
||||
pub icon: Option<String>,
|
||||
}
|
||||
use crate::{Notification, NotificationData, PermissionState, Result};
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn is_permission_granted<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
notification: State<'_, Notification<R>>,
|
||||
) -> Result<bool> {
|
||||
notification
|
||||
.permission_state()
|
||||
.map(|s| s == PermissionState::Granted)
|
||||
) -> Result<Option<bool>> {
|
||||
let state = notification.permission_state()?;
|
||||
match state {
|
||||
PermissionState::Granted => Ok(Some(true)),
|
||||
PermissionState::Denied => Ok(Some(false)),
|
||||
PermissionState::Unknown => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
#[command]
|
||||
@@ -40,15 +31,9 @@ pub(crate) async fn request_permission<R: Runtime>(
|
||||
pub(crate) async fn notify<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
notification: State<'_, Notification<R>>,
|
||||
options: NotificationOptions,
|
||||
options: NotificationData,
|
||||
) -> Result<()> {
|
||||
let mut builder = notification.builder().title(options.title);
|
||||
if let Some(body) = options.body {
|
||||
builder = builder.body(body);
|
||||
}
|
||||
if let Some(icon) = options.icon {
|
||||
builder = builder.icon(icon);
|
||||
}
|
||||
|
||||
let mut builder = notification.builder();
|
||||
builder.data = options;
|
||||
builder.show()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user