Knowledge Base

Native Windows Notifications with Delphi

Windows 10 and 11 have their own notification system. In a recent Delphi release, a component called TNotificationCenter was added. Add this to your form, and you can display native Windows notifications.

Example:

const
  UPDATE_NOTIFICATION_ID = 1;

procedure TForm1.Button1Click;
var
  Notification: TNotification;
begin
  Notification := NotificationCenter1.CreateNotification;

  Notification.Name := 'MyName';
  Notification.Number := UPDATE_NOTIFICATION_ID;
  Notification.AlertBody := 'Update 2.3.5 is available, click here to install the update now.';
  Notification.EnableSound := False;
  Notification.FireDate := Now;
  Notification.HasAction := True;

  NotificationCenter1.PresentNotification(Notification);
end;

To handle a notification click, we implement OnReceiveLocalNotification

procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject; ANotification: TNotification);
begin
  if ANotification.Number = UPDATE_NOTIFICATION_ID  then
  begin
    InstallUpdate;
  end;
end;

 

To schedule a notification, we use NotificationCenter.ScheduleNotification

procedure ScheduleNotification;
var
  Notification: TNotification;
begin
  Notification := NotificationCenter1.CreateNotification;
  Notification.AlertBody := 'It''s time to drink coffee :-)';
  Notification.FireDate  := IncHour(Now);
  
  NotificationCenter1.ScheduleNotification(Notification);
end;

 

and to cancel notification(s):

procedure CancelNotifications;
begin
  // Cancel a specific one
  NotificationCenter1.CancelNotification('MyName');
  
  // Cancel all notifications
  NotificationCenter1.CancelAll;
end;

Written by David Paul van der Wulp
Delphi developer

Contact

Let us help you to realise your ambitions

GDK Software UK

(+44) 20 3355 4470

GDK Software USA

+1 (575) 733-5744