tauri
tauri copied to clipboard
[feat] Notification onclick event
Describe the problem
I am running my Tauri app in the tray and I only want to show the UI when the user responds to a notification. I can't seem to subscribe to any notification onclick event.
Describe the solution you'd like
I would like to be able to subscribe to a click event, like so:
let n = new Notification('title', { body: 'some text' });
n.onclick = function() { console.log('click happened') };
Alternatives considered
No response
Additional context
No response
Click events aren't implemented yet.
Is there any ETA on this?
I don't know if this belongs in the same issue, but click focuses the (main) window for me on Ubuntu and macOS, but not on Windows (10). It would be nice to be consistent if possible.
I unsuccessfully tried to figure out whether rust-notify supports this.
Anything that can be done to develop this? New to Open Source if any references and guides to help then I would like to help with this feature.
@Huzaifa-MS the problem is that the upstream notification crates (https://github.com/hoodie/notify-rust and its dependencies) don't support this, so you'd have to work with ugly system APIs to implement this.
@FabianLars I see. I did get that vibe from searching for a bit. I also noticied that the web api for notification works on its own in the frontend. Is it possible to use that to create actions in notifications? Gonna be looking into that later
I also noticied that the web api for notification works on its own in the frontend
It doesn't. We're injecting our own api (the same from the api module) here: https://github.com/tauri-apps/tauri/blob/dev/core/tauri/scripts/core.js#L163-L247
It doesn't.
I am sorry, I am new to this so perhaps I am not understanding. So this code I wrote in the frontend portion of the Tauri app:
const callWebNotificationAPI = async () => {
Notification
if (!("Notification" in window)) {
// Check if the browser supports notifications
alert("This browser does not support desktop notification")
} else if (Notification.permission === "granted") {
// Check whether notification permissions have already been granted;
// if so, create a notification
new Notification("Hi there!")
// …
} else if (Notification.permission !== "denied") {
// We need to ask the user for permission
Notification.requestPermission().then((permission) => {
// If the user accepts, let's create a notification
if (permission === "granted") {
new Notification("Hi there!")
}
})
}
}
Is getting transformed by Tauri automatically at compile time to use Tauri's notification library instead?
Does Tauri not support this web API yet? Again apologies for my lack of knowledge on Tauri and the associated technologies.
Is getting transformed by Tauri automatically at compile time to use Tauri's notification library instead?
We don't touch your code at all. We overwrite the global Notification
/ window.Notification
object/apis.
Does Tauri not support this web API yet?
Tauri does. But the underlying webviews don't, by design as far as i know. They expect the host process to handle/implement notifications, which we do by overwriting the js apis to use our rust api.
I see. I understand better now. Thank you.
@
@Huzaifa-MS the problem is that the upstream notification crates (https://github.com/hoodie/notify-rust and its dependencies) don't support this, so you'd have to work with ugly system APIs to implement this.
Do you of any guides or documentation I can look into to implement the feature? I would like to try my hand at it.
Is there any way to use the JS APIs instead?
There are no JS APIs to use. The web apis you know from normal browsers don't exist in webviews and we need to polyfill them ourselves. And the nodejs apis can't be used because there is no nodejs runtime in tauri.
Or did you mean something different?
The web apis you know from normal browsers don't exist in webviews and we need to polyfill them ourselves
Completely understood, thank you. The lack of click handlers is really only a problem on MacOS and why I had hoped to pipe notifications through a webview. Other platforms seem to respect the actions
available in notify-rust
.