Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Notification onclick event #3698

Open
kmcluckie opened this issue Mar 14, 2022 · 14 comments
Open

[feat] Notification onclick event #3698

kmcluckie opened this issue Mar 14, 2022 · 14 comments

Comments

@kmcluckie
Copy link

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

@lucasfernog
Copy link
Member

Click events aren't implemented yet.

@jmsariron
Copy link

Is there any ETA on this?

@betamos
Copy link
Contributor

betamos commented Jun 30, 2022

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.

@Huzaifa-MS
Copy link

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.

@FabianLars
Copy link
Sponsor Member

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

@Huzaifa-MS
Copy link

@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

@FabianLars
Copy link
Sponsor Member

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

@Huzaifa-MS
Copy link

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.

@FabianLars
Copy link
Sponsor Member

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.

@Huzaifa-MS
Copy link

I see. I understand better now. Thank you.

@Huzaifa-MS
Copy link

@

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

@jamesacklin
Copy link

Is there any way to use the JS APIs instead?

@FabianLars
Copy link
Sponsor Member

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?

@jamesacklin
Copy link

jamesacklin commented Jan 27, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📬Proposal
Development

No branches or pull requests

7 participants