Skip to content

Commit 8941790

Browse files
authored
fix(core): notification permission check when !allowlisted, closes #1666 (#1677)
1 parent 8b6f3de commit 8941790

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
`Notification.requestPermission()` now returns `"denied"` when not allowlisted.
6+
`IsNotificationPermissionGranted` returns `false` when not allowlisted.

core/tauri/src/endpoints/notification.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ use serde::Deserialize;
88
#[cfg(notification_all)]
99
use crate::api::notification::Notification;
1010

11+
// `Granted` response from `request_permission`. Matches the Web API return value.
12+
#[cfg(notification_all)]
13+
const PERMISSION_GRANTED: &str = "granted";
14+
// `Denied` response from `request_permission`. Matches the Web API return value.
15+
const PERMISSION_DENIED: &str = "denied";
16+
1117
/// The options for the notification API.
1218
#[derive(Deserialize)]
1319
pub struct NotificationOptions {
@@ -43,13 +49,13 @@ impl Cmd {
4349
#[cfg(notification_all)]
4450
return is_permission_granted().map(Into::into);
4551
#[cfg(not(notification_all))]
46-
Err(crate::Error::ApiNotAllowlisted("notification".to_string()))
52+
Ok(false.into())
4753
}
4854
Self::RequestNotificationPermission => {
4955
#[cfg(notification_all)]
5056
return request_permission().map(Into::into);
5157
#[cfg(not(notification_all))]
52-
Err(crate::Error::ApiNotAllowlisted("notification".to_string()))
58+
Ok(PERMISSION_DENIED.into())
5359
}
5460
}
5561
}
@@ -81,10 +87,12 @@ pub fn is_permission_granted() -> crate::Result<InvokeResponse> {
8187
#[cfg(notification_all)]
8288
pub fn request_permission() -> crate::Result<String> {
8389
let mut settings = crate::settings::read_settings()?;
84-
let granted = "granted".to_string();
85-
let denied = "denied".to_string();
8690
if let Some(allow_notification) = settings.allow_notification {
87-
return Ok(if allow_notification { granted } else { denied });
91+
return Ok(if allow_notification {
92+
PERMISSION_GRANTED.to_string()
93+
} else {
94+
PERMISSION_DENIED.to_string()
95+
});
8896
}
8997
let answer = crate::api::dialog::ask(
9098
"Permissions",
@@ -94,12 +102,12 @@ pub fn request_permission() -> crate::Result<String> {
94102
crate::api::dialog::AskResponse::Yes => {
95103
settings.allow_notification = Some(true);
96104
crate::settings::write_settings(settings)?;
97-
Ok(granted)
105+
Ok(PERMISSION_GRANTED.to_string())
98106
}
99107
crate::api::dialog::AskResponse::No => {
100108
settings.allow_notification = Some(false);
101109
crate::settings::write_settings(settings)?;
102-
Ok(denied)
110+
Ok(PERMISSION_DENIED.to_string())
103111
}
104112
}
105113
}

0 commit comments

Comments
 (0)