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
+11 -26
View File
@@ -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()
}