tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[feat] Notification onclick event

Open kmcluckie opened this issue 2 years ago • 14 comments

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

kmcluckie avatar Mar 14 '22 12:03 kmcluckie

Click events aren't implemented yet.

lucasfernog avatar Mar 14 '22 13:03 lucasfernog

Is there any ETA on this?

jmsariron avatar May 16 '22 12:05 jmsariron

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.

betamos avatar Jun 30 '22 21:06 betamos

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 avatar Aug 25 '22 02:08 Huzaifa-MS

@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 avatar Aug 26 '22 17:08 FabianLars

@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

Huzaifa-MS avatar Sep 01 '22 09:09 Huzaifa-MS

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

FabianLars avatar Sep 01 '22 09:09 FabianLars

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.

Huzaifa-MS avatar Sep 01 '22 20:09 Huzaifa-MS

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.

FabianLars avatar Sep 02 '22 09:09 FabianLars

I see. I understand better now. Thank you.

Huzaifa-MS avatar Sep 02 '22 09:09 Huzaifa-MS

@

@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.

Huzaifa-MS avatar Sep 02 '22 22:09 Huzaifa-MS

Is there any way to use the JS APIs instead?

jamesacklin avatar Jan 27 '23 15:01 jamesacklin

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?

FabianLars avatar Jan 27 '23 15:01 FabianLars

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.

jamesacklin avatar Jan 27 '23 17:01 jamesacklin