简体   繁体   中英

Windows 10 Toast Notifications Desktop Application

I'm trying to integrate some Windows 10 features into my existing Windows Desktop application. I am a little stuck integrating the Toast Notifications. Using the toast notification example ( https://code.msdn.microsoft.com/windowsdesktop/sending-toast-notifications-71e230a2/ ) I was able to implement code to send and hide notifications. It also works, that when the user clicks on an 'active' notification an event handler in my application is invoked.

However, as soon as the notification is 'archived' in the 'Action Center', nothing happens when the user clicks on my notification. How can I react to clicks in such situations?

Thanks for your help,

Lukas

I have developed WinToast , a library written in C++ to integrate Windows Toast Notification easily. I have used it to integrate Toast notifications in different projects, specially with Qt Framework.

The native Toast Notification needs some functions of the Com Fundamentals which are availables only in moderns version of Windows (minimum supported client: Windows 8).

That's why the library loads all the required libraries dynamically. Make your application compatible with older versions of Windows using WinToast. There is an attached example explaining how to use it in the repository.

To show a toast, just create the template and your custom handler and launch it:

WinToastHandlerExample* handler = new WinToastHandlerExample;
WinToastTemplate templ  = WinToastTemplate(WinToastTemplate::ImageWithTwoLines);
templ.setImagePath(L"C:/example.png");
templ.setTextField(L"title", WinToastTemplate::FirstLine);
templ.setTextField(L"subtitle", WinToastTemplate::SecondLine);

if (!WinToast::instance()->showToast(templ, handler)) {
   std::wcout << L"Could not launch your toast notification!";
}

There's updated documentation for Windows 10 describing how to use Action Center (and interactive toasts) from a Win32 app: https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast-desktop

Basically, you have to use a COM server. The Activated event on the ToastNotification itself is a runtime event... useless if your program has been closed and the user clicks on your toast from Action Center. Thus, Activated only fires if the user clicks your toast when it first pops up. It does NOT fire when the user clicks your toast from Action Center. That's what the COM server is for (or what the OnActivated method in UWP apps is for).

That sample is for Windows 8; the Action Centre in Windows 10 Tech Preview is new and there is no SDK out yet for you to use any new features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM