- 1 year ago
How to let a toast notification stay in Windows 10 Action Center with Delphi
To keep a toast notification visible in the Windows 10 Action Center using Delphi, you can use the Windows Runtime API to interact with the notification system. Here's a step-by-step guide on how to achieve this:
Import Windows Runtime API:
First, you need to import the Windows Runtime API for toast notifications. Add the following unit to your Delphi project:delphiuses
Winapi.Data.WinRT, Winapi.WebRT, Winapi.Foundation;
Create and Display Toast Notification:
Next, you can create and display the toast notification. The following code demonstrates how to create a simple toast notification and show it:delphiprocedure ShowToastNotification(const Text: string);
var
ToastXML: HSTRING;
ToastNotifier: IToastNotifier;
ToastNotification: IToastNotification;
ToastFactory: IToastNotificationFactory;
begin
// Create the toast notification XML
ToastXML := StringToHString('<toast><visual><binding template="ToastText01">' +
'<text id="1">' + Text + '</text></binding></visual></toast>');
// Get the ToastNotificationManager
if Succeeded(RoGetActivationFactory(StringToHString('Windows.UI.Notifications.ToastNotificationManager'),
IToastNotificationManagerStatics, ToastFactory)) then
begin
// Create the toast notifier
ToastNotifier := ToastFactory.CreateToastNotifier(StringToHString('YourAppName'));
// Create the toast notification
if Succeeded(ToastFactory.CreateToastNotification(ToastXML, ToastNotification)) then
begin
// Show the toast notification
ToastNotifier.Show(ToastNotification);
end;
end;
end;
Keep Toast in Action Center:
By default, a toast notification is displayed briefly and then moved to the Action Center. To keep the toast notification visible in the Action Center until the user dismisses it, you need to add the "duration" attribute to the toast notification XML and set it to "long":delphiToastXML := StringToHString('<toast duration="long"><visual><binding template="ToastText01">' +
'<text id="1">' + Text + '</text></binding></visual></toast>');
With this change, the toast notification will remain in the Action Center until the user manually dismisses it.
Remember that toast notifications are intended for lightweight and non-intrusive communication with the user. Keeping notifications in the Action Center for a long time may not be the best user experience. Use this feature thoughtfully and avoid overwhelming users with too many persistent notifications.
Please note that this code uses the Windows Runtime API and is designed for Windows 10 and above. Ensure that you are using a compatible version of Delphi and targeting the correct Windows platform.