Back to Qgis

README

external/wintoast/README.md

latest11.7 KB
Original Source


WinToast

WinToast is a lightly library written in C++ which brings a complete integration of the modern toast notifications of Windows 8 & Windows 10.

Toast notifications allows your app to inform the users about relevant information and timely events that they should see and take action upon inside your app, such as a new instant message, a new friend request, breaking news, or a calendar event.

  1. Toast Templates
  2. Event Handler
  3. Expiration Time
  4. Additional features available on Windows 10
  5. Error Handling
  6. Example of usage
  7. Toast configuration on Windows 10
  8. Projects using WinToast
<div id='id1' />

Toast Templates

WinToast integrates all standard templates available in the ToastTemplateType enumeration.

TemplateDescriptionExample
ImageAndText01A large image and a single string wrapped across three lines of text.
ImageAndText02A large image, one string of bold text on the first line, one string of regular text wrapped across the second and third lines.
ImageAndText03A large image, one string of bold text wrapped across the first two lines, one string of regular text on the third line.
ImageAndText04A large image, one string of bold text on the first line, one string of regular text on the second line, one string of regular text on the third line.
Text01Single string wrapped across three lines of text.
Text02One string of bold text on the first line, one string of regular text wrapped across the second and third lines.
Text03One string of bold text wrapped across the first two lines, one string of regular text on the third line.
Text04One string of bold text on the first line, one string of regular text on the second line, one string of regular text on the third line.

Example of a ImageAndText02 template:

cpp
WinToastTemplate templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
templ.setTextField(L"title", WinToastTemplate::FirstLine);
templ.setTextField(L"subtitle", WinToastTemplate::SecondLine);
templ.setImagePath(L"C:/example.png"); 

Note: The user can use the default system sound or specify a sound to play when a toast notification is displayed. Same behavior for the toast notification image, by default Windows try to use the app icon.*

<div id='id3' />

Event Handler

WinToast handle different events:

  • Activated: Occurs when user activates a toast notification through a click or touch. Apps that are running subscribe to this event
  • Dismissed: Occurs when a toast notification leaves the screen, either by expiring or being explicitly dismissed by the user.
    • Application Hidden: The application hid the toast using ToastNotifier.hide.
    • User Canceled: The user dismissed the toast.
    • Timed Out: The toast has expired
  • Failed: Occurs when an error is caused when Windows attempts to raise a toast notification.

Create your custom handler to interact with the user actions by subclassing the interface IWinToastHandler:

cpp
class WinToastHandlerExample : public IWinToastHandler {
 public:
	WinToastHandlerExample(); 
	// Public interfaces
	void toastActivated() const override;
	void toastDismissed(WinToastDismissalReason state) const override;
	void toastFailed() const override;
 };
<div id='id4' />

Expiration Time

Set the time after which a toast notification is no longer considered current or valid and should not be displayed. Windows attempts to raise toast notifications immediately after you call Show, so this property is rarely used.

For Windows 8.x app, this property also causes the toast notification to be removed from the Action Center once the specified data and time is reached.

Note: Default Windows behavior is to hide notification automatically after time set in Windows Ease of Access Settings. If you need to preserve notification in Windows Action Center for longer period of time, you have to call WinToastTemplate::setExpiration method.

<div id='id5' />

Additional features available on Windows 10

If your system supports the new modern features (Version > Windows 8.1) available in Windows 10, you can add some interesting fields as:

  • Actions: you can add your own actions, this fact allow you to interact with user in a different way:
cpp
WinToastTemplate templ = WinToastTemplate(WinToastTemplate::Text02);
templ.setTextField(L"Do you think this feature is cool?", WinToastTemplate::FirstLine);
templ.setTextField(L"Ofc,it is!", WinToastTemplate::SecondLine);

std::vector<std::wstring> actions;
actions.push_back(L"Yes");
actions.push_back(L"No");
for (auto const &action : actions)	
    templ.addAction(action);
WinToast::instance()->showToast(templ, handler) 

  • Attribution text: you can add/remove the attribution text, by default is empty. Use WinToastTemplate::setAttributionText to modify it.
  • Duration: The amount of time the toast should display. This attribute can have one of the following values: - System: default system configuration.
    • Short: default system short time configuration.
    • Long: default system long time configuration.
  • Audio Properties: you can modify the different behaviors of the sound:
    • Default: plays the audio file just one time.
    • Silent: turn off the sound.
    • Loop: plays the given sound in a loop during the toast existence.

WinToast allows the modification of the default audio file. Add the given file in to your projects resources (must be ms-appx:// or ms-appdata:// path) and define it by calling: WinToastTemplate::setAudioPath

By default, WinToast checks if your systems support the features, ignoring the not supported ones.

<div id='id2' />

Error Handling

There are several reasons WinToast can fail that's why the library notifies caller about fail reason. Those are the code for each failure:

WinToastErrorError CodeError message
NoError0x00No error. The process was executed correctly
NotInitialized0x01The library has not been initialized
SystemNotSupported0x02The OS does not support WinToast
ShellLinkNotCreated0x03The library was not able to create a Shell Link for the app
InvalidAppUserModelID0x04The AUMI is not a valid one
InvalidParameters0x05The parameters used to configure the library are not valid normally because an invalid AUMI or App Name
NotDisplayed0x06The toast was created correctly but WinToast was not able to display the toast
UnknownError0x07Unknown error

A common example of usage is to check while initializing the library or showing a toast notification the possible failure code:

cpp
WinToast::WinToastError error;
const bool succeeded = WinToast::instance()->initialize(&error);
if (!succeeded) {  
    std::wcout << L"Error, could not initialize the lib. Error number: " 
    << error << std::endl;
}
...
// Configure the template
...
const bool launched = WinToast::instance()->showToast(templ, handler, &error);
if (!launched) {
    std::wcout << L"Error: Could not launch your toast notification. Error: "
     << error << std::endl;
}
<div id='id6' />

Example of Usage

For an easy usage, you can just use the available singleton instance.

First step, Import the header file wintoastlib.h to your project. You should check if your Windows Version is supported by the library.

cpp
using namespace WinToastLib;
....
if (!WinToast::isCompatible()) {
    std::wcout << L"Error, your system in not supported!" << std::endl;
}

Configure your App User Model Id, this can be done by using the existing helper:

cpp

WinToast::instance()->setAppName(L"WinToastExample");
const auto aumi = WinToast::configureAUMI(L"mohabouje", L"wintoast", L"wintoastexample", L"20161006");
WinToast::instance()->setAppUserModelId(aumi);	

Initialize all the dependencies and check if WinToast has been initialized successfully in your system:

cpp
if (!WinToast::instance()->initialize()) {
  std::wcout << L"Error, could not initialize the lib!" << std::endl;
}

Implement your own action handler by subclassing the interface IWinToastHandler and custom your template:

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

Finally show the results:

cpp

if (!WinToast::instance()->showToast(templ, handler)) {
    std::wcout << L"Error: Could not launch your toast notification!" << std::endl;
}
<div id='id7' />

Toast configuration on Windows 10

Windows allows the configuration of the default behavior of a toast notification. This can be done in the Ease of Access configuration by modifying the Other options tab.

The system configuration helps you to define how long you want notifications to appear for (5 seconds to 5 minutes) as turning on visual notifications for sound.

<div id='id8' />

Projects using WinToast

  • Git for Windows: A fork of Git containing Windows-specific patches.
  • nheko: Desktop client for the Matrix protocol.
  • EDPathFinder: A program that creates an optimal route that passes through two or more systems in Elite.
  • AntiExploit: antiexploit utility for Windows.
  • Zroya: Python extension for creating native Windows notifications..
  • PidginWinToastNotifications: Windows Toast Notification Plugin for Pidgin.
  • Dnai-Editor: Visual Scripting, node editor.