electron icon indicating copy to clipboard operation
electron copied to clipboard

Windows Toast notifications activation issues

Open vyunikov opened this issue 5 years ago • 9 comments

Preflight Checklist

  • [x] I have read the Contributing Guidelines for this project.
  • [x] I agree to follow the Code of Conduct that this project adheres to.
  • [x] I have searched the issue tracker for a feature request that matches the one I want to file, without success.

Problem Description

The way toast notifications support is implemented on Windows doesn't feel quite right. The main issue I see is that it doesn't integrate well with Windows Action Center. I can highlight the following Action Center integration issues:

  1. Once notification is displayed and moves to the Action Center, click events are not delivered to the app.
  2. Notifications don't persist in the Action Center. If you open the Action Center once notification is displayed, you'll see the notification there, but the next time you open it - you'll see that notification is missing.
  3. App can't be activated from the Action Center, i.e. when notification is clicked app window is not being brought to the foreground, and when app isn't running you can't start it by clicking on notification.

There are several issues which mention the problems I described above: #20415 #21610

There were more issues closed as they were reported on an older electron versions.

All this makes the app which uses electron toast notifications feel broken. I know that some people use userland packages like electron-windows-notifications and electron-windows-interactive-notifications as they implement full support of toasts on Windows.

I looked in the code which implements toast notifications on Windows here: https://github.com/electron/electron/blob/master/shell/browser/notifications/win/windows_toast_notification.cc

I see that it doesn't implement COM Activator. Besides that the way activation callback is registered, is going to work only when the process is running, it can't start the process from notification click.

Proposed Solution

There are a few examples from Microsoft which mention how to implement that correctly: https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast-desktop https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast-desktop-cpp-wrl

I propose to implement COM activator and expose APIs for activator registration in Windows registry and configuring app shortcut with activator GUID. It might also be required to update toast XML with launch property of toast element. Also, it will be required to update electron-squirrel-startup and electron-winstaller packages to automatically handle registration mentioned above.

On a related note, I think it would also be good to allow specifying user data string, which will be attached to toast XML and can be passed back along with click event.

As I have prior experience with toast notifications implementation in another app on C++, I would like to implement a Pull Request for that

Alternatives Considered

As an alternative it's also possible to implement activation using protocol activation type. This way it will be required to register custom protocol handler which will be used to activate the app, but COM components registration stuff won't be required.

Additional Information

I would highly appreciate any feedback from other electron contributors before I start working on the PR for that: opinions on the solution proposed, thoughts about complications of installation process. Also would be good to understand the reasoning behind implementation of notification support this way instead of implementing COM activator or protocol activation.

vyunikov avatar Jul 12 '20 17:07 vyunikov

@vyunikov You're absolutely right that a COM-based notification system would be superior to the one we have currently implemented.

Many Electron apps are currently using the protocol-based activation you mention, but we've known that to be difficult for Electron developers and a bit clumsy.

As the author of the two userland packages you mention, I'd applaud and thank you for making them obsolete by getting a COM-based system into Electron.

What do you need from us to get started?

felixrieseberg avatar Jul 14 '20 21:07 felixrieseberg

A "green light" from you is everything I need for now.

vyunikov avatar Jul 14 '20 21:07 vyunikov

Not sure we'll be able to make the packages you mentioned obsolete (at least for now), as I thought that as a first stage let's just make COM-based activation working. We may add support for extended XML schemes and custom actions later on.

vyunikov avatar Jul 14 '20 21:07 vyunikov

I just ran into the issue where a notification from the action center does not send click events to the app and was wondering what the recommended way to fix that for Electron 15?

EDIT: I see that notifications created via the HTML5-based Notifications API also suffers from the same problem. I assume the same underlying native API is used to create the notification? In any case, thought I would mention that I am using the Notification class in the main process.

praneetloke avatar Nov 06 '21 04:11 praneetloke

I'm having the this issue using electron v25.0.1.

The following, in the main thread, with show a notification on MacOS and Windows, and react to a click on Mac and the windows toast notification. But NOT on windows from the Action/Notification center.

const { Notification } = require('electron');
let n = new Notification({title: "Hello", body: "Testing testing..."});
n.on('click', e => console.log("CLICK!"));
n.show();

I see a number of issues (#18746, #29461) around this topic here and on stackoverflow/etc. Is there any movement or a solution for this?

nicholasstephan avatar Jun 05 '23 20:06 nicholasstephan

I have the same issue !

mlp-dw avatar Jul 06 '23 08:07 mlp-dw

Has anyone found a solution to this problem?

major697 avatar Feb 18 '24 08:02 major697

The most straightforward way at the moment is to use a custom protocol to launch an app as described here

jameshelou avatar Feb 18 '24 22:02 jameshelou

I don't know if it helps. But I found that this issue is still relevant when using the web Notifications API. If a notification is created & shown in the renderer process, the click event is handled by the browser (Chromium) and specifically the notification_event_dispatcher_impl.cc handles the event. Now once a notification moves to the Action Center and thus "disappears" the "DispatchNotificationCloseEvent" is fired and the notification is removed from the listeners list. So if after that event the notification is clicked in the Action Center, from the browser point of view the notification has closed, so the click event is not handled anymore. AFAIK Windows notifications are "closed" when they are clicked on, so is it possible to just not register the "toast->add_Dismissed" in windows_toast_notification.cc? Does anyone know the consequences this change would have?

I tested this in Windows 11 Pro build 22631.4317

lokhan avatar Oct 11 '24 11:10 lokhan

I am not sure if something has changed in recent Electron versions (perhaps version 37+) or if a recent Windows 11 update made things better. But clicking notifications seems to work for me on Windows 11. I haven't changed anything in my app other than update the Electron version with each new release. I will pay close attention for a bit more and report back if it's really resolved somehow or if it's a temporary fluke.

EDIT: Ah I just remembered the nuance of this issue. It's been so long that I forgot about it. I read the description to refresh my memory and reproduced it. I forgot that notifications lose their interactivity once they end up in the action center. If I click the notification while it is still displaying in the bottom right corner when it first pops-up, then the click handler does fire.

praneetloke avatar Oct 10 '25 04:10 praneetloke