Skip to content

Commit 239bba5

Browse files
committed
refactor(core): check notification permission on the Rust endpoint [TRI-017] (#23)
1 parent a48b8b1 commit 239bba5

5 files changed

Lines changed: 78 additions & 60 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
The notification endpoint now checks for the permission flag and requests if the value is not set.

core/tauri/scripts/core.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,26 +210,20 @@
210210
Object.freeze(options)
211211
}
212212

213-
isPermissionGranted().then(function (permission) {
214-
if (permission) {
215-
return window.__TAURI_INVOKE__(
216-
'tauri',
217-
{
218-
__tauriModule: 'Notification',
219-
message: {
220-
cmd: 'notification',
221-
options:
222-
typeof options === 'string'
223-
? {
224-
title: options
225-
}
226-
: options
227-
}
228-
},
229-
_KEY_VALUE_
230-
)
231-
}
232-
})
213+
return window.__TAURI_INVOKE__(
214+
'tauri', {
215+
__tauriModule: 'Notification',
216+
message: {
217+
cmd: 'notification',
218+
options: typeof options === 'string' ?
219+
{
220+
title: options
221+
} :
222+
options
223+
}
224+
},
225+
_KEY_VALUE_
226+
)
233227
}
234228

235229
window.Notification = function (title, options) {

core/tauri/src/endpoints/notification.rs

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ impl Cmd {
4545
context: InvokeContext<R>,
4646
options: NotificationOptions,
4747
) -> crate::Result<()> {
48+
let allowed = match is_permission_granted(&context) {
49+
Some(p) => p,
50+
None => request_permission(&context),
51+
};
52+
if !allowed {
53+
return Err(crate::Error::NotificationNotAllowed);
54+
}
4855
let mut notification =
4956
Notification::new(context.config.tauri.bundle.identifier.clone()).title(options.title);
5057
if let Some(body) = options.body {
@@ -61,39 +68,7 @@ impl Cmd {
6168
fn request_notification_permission<R: Runtime>(
6269
context: InvokeContext<R>,
6370
) -> crate::Result<&'static str> {
64-
let mut settings = crate::settings::read_settings(
65-
&context.config,
66-
&context.package_info,
67-
context.window.state::<Env>().inner(),
68-
);
69-
if let Some(allow_notification) = settings.allow_notification {
70-
return Ok(if allow_notification {
71-
PERMISSION_GRANTED
72-
} else {
73-
PERMISSION_DENIED
74-
});
75-
}
76-
let (tx, rx) = std::sync::mpsc::channel();
77-
crate::api::dialog::ask(
78-
Some(&context.window),
79-
"Permissions",
80-
"This app wants to show notifications. Do you allow?",
81-
move |answer| {
82-
tx.send(answer).unwrap();
83-
},
84-
);
85-
86-
let answer = rx.recv().unwrap();
87-
88-
settings.allow_notification = Some(answer);
89-
crate::settings::write_settings(
90-
&context.config,
91-
&context.package_info,
92-
context.window.state::<Env>().inner(),
93-
settings,
94-
)?;
95-
96-
if answer {
71+
if request_permission(&context) {
9772
Ok(PERMISSION_GRANTED)
9873
} else {
9974
Ok(PERMISSION_DENIED)
@@ -111,12 +86,7 @@ impl Cmd {
11186
fn is_notification_permission_granted<R: Runtime>(
11287
context: InvokeContext<R>,
11388
) -> crate::Result<Option<bool>> {
114-
let settings = crate::settings::read_settings(
115-
&context.config,
116-
&context.package_info,
117-
context.window.state::<Env>().inner(),
118-
);
119-
if let Some(allow_notification) = settings.allow_notification {
89+
if let Some(allow_notification) = is_permission_granted(&context) {
12090
Ok(Some(allow_notification))
12191
} else {
12292
Ok(None)
@@ -130,3 +100,46 @@ impl Cmd {
130100
Ok(Some(false))
131101
}
132102
}
103+
104+
#[cfg(notification_all)]
105+
fn request_permission<R: Runtime>(context: &InvokeContext<R>) -> bool {
106+
let mut settings = crate::settings::read_settings(
107+
&context.config,
108+
&context.package_info,
109+
context.window.state::<Env>().inner(),
110+
);
111+
if let Some(allow_notification) = settings.allow_notification {
112+
return allow_notification;
113+
}
114+
let (tx, rx) = std::sync::mpsc::channel();
115+
crate::api::dialog::ask(
116+
Some(&context.window),
117+
"Permissions",
118+
"This app wants to show notifications. Do you allow?",
119+
move |answer| {
120+
tx.send(answer).unwrap();
121+
},
122+
);
123+
124+
let answer = rx.recv().unwrap();
125+
126+
settings.allow_notification = Some(answer);
127+
let _ = crate::settings::write_settings(
128+
&context.config,
129+
&context.package_info,
130+
context.window.state::<Env>().inner(),
131+
settings,
132+
);
133+
134+
answer
135+
}
136+
137+
#[cfg(notification_all)]
138+
fn is_permission_granted<R: Runtime>(context: &InvokeContext<R>) -> Option<bool> {
139+
crate::settings::read_settings(
140+
&context.config,
141+
&context.package_info,
142+
context.window.state::<Env>().inner(),
143+
)
144+
.allow_notification
145+
}

core/tauri/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ pub enum Error {
8484
/// Path not allowed by the scope.
8585
#[error("path not allowed on the configured scope: {0}")]
8686
PathNotAllowed(PathBuf),
87+
/// The user did not allow sending notifications.
88+
#[error("sending notification was not allowed by the user")]
89+
NotificationNotAllowed,
8790
}
8891

8992
impl From<serde_json::Error> for Error {

examples/api/src/components/Notifications.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<script>
22
export let onMessage;
33
4+
// send the notification directly
5+
// the backend is responsible for checking the permission
46
function _sendNotification() {
57
new Notification("Notification title", {
68
body: "This is the notification body",
79
});
810
}
911
12+
// alternatively, check the permission ourselves
1013
function sendNotification() {
1114
if (Notification.permission === "default") {
1215
Notification.requestPermission()
@@ -26,6 +29,6 @@
2629
}
2730
</script>
2831

29-
<button class="button" id="notification" on:click={sendNotification}>
32+
<button class="button" id="notification" on:click={_sendNotification}>
3033
Send test notification
3134
</button>

0 commit comments

Comments
 (0)